Skip to content

Commit

Permalink
Merge pull request #59 from eX-Mech/pydantic-to-attrs
Browse files Browse the repository at this point in the history
Pydantic to attrs
  • Loading branch information
ashwinvis committed Oct 14, 2022
2 parents 2cb0b50 + a4455ad commit b11e076
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 39 deletions.
1 change: 0 additions & 1 deletion .github/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ channels:

dependencies:
- xarray
- pydantic
3 changes: 1 addition & 2 deletions .github/environment_pypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ channels:
- conda-forge

dependencies:
- python=3.7[build=*pypy]
- python=3.9[build=*pypy]
- xarray
- pydantic
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,8 @@ jobs:
shell: bash -l {0}
run: |
python -m pytest -s -v -n auto
- name: Run mypy
shell: bash -l {0}
run: |
python -m mypy ./pymech --show-error-codes
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ repos:
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 22.3.0
rev: 22.10.0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2 # pick a git hash / tag to point to
- repo: https://github.com/pycqa/flake8
rev: 5.0.4
hooks:
- id: flake8
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Security in case of vulnerabilities.

### Added

- Dataclass {class}`pymech.neksuite.field.Header` using [`pydantic`](https://pydantic-docs.helpmanual.io/) for reading file headers
- Dataclass {class}`pymech.neksuite.field.Header` using [`attrs`](https://www.attrs.org/en/stable/) for reading file headers
- Function {func}`pymech.neksuite.field.read_header`
- Module {mod}`pymech.meshtools` for manipulating meshes
- Xarray backend ``pymech``. See {ref}`dataset`
Expand Down
4 changes: 2 additions & 2 deletions docs/asv_bench/results/benchmarks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"number": 0,
"param_names": [],
"params": [],
"processes": 2,
"repeat": 0,
"rounds": 2,
"sample_time": 0.01,
"timeout": 60.0,
"type": "time",
Expand All @@ -22,8 +22,8 @@
"number": 0,
"param_names": [],
"params": [],
"processes": 2,
"repeat": 0,
"rounds": 2,
"sample_time": 0.01,
"timeout": 60.0,
"type": "time",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"commit_hash": "2cb0b50e87c98df6df575f61d9b244abc6886d62", "env_name": "virtualenv-py3.8-dask[array]-numpy-pandas", "date": 1655662291000, "params": {"arch": "x86_64", "cpu": "AMD Ryzen 7 4800H with Radeon Graphics", "machine": "tuxpulse", "num_cpu": "16", "os": "Linux 5.15.0-10033-tuxedo", "ram": "16GB", "python": "3.8", "numpy": "", "dask[array]": "", "pandas": ""}, "python": "3.8", "requirements": {"numpy": "", "dask[array]": "", "pandas": ""}, "env_vars": {}, "result_columns": ["result", "params", "version", "started_at", "duration", "stats_ci_99_a", "stats_ci_99_b", "stats_q_25", "stats_q_75", "stats_number", "stats_repeat", "samples", "profile"], "results": {"bench_open_dataset.OpenDatasetSuite.time_read": [[0.6610401119996823], [], "d8736c9050c8133f745cc20f6d35fcbac47a834c114c581fff6c8b4123f99d6a", 1655665708407, 8.1294, [0.65864], [0.6679], [0.66033], [0.66239], [1], [10]], "bench_readnek.ReadnekSuite.time_read": [[0.0306186504985817], [], "4247c82d15af70d066d71b05656858fa8f9088d8df24e2be77eb30b987a05a4b", 1655665712473, 0.61015, [0.030559], [0.031918], [0.03059], [0.030657], [1], [10]]}, "durations": {}, "version": 2}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"commit_hash": "2cb0b50e87c98df6df575f61d9b244abc6886d62", "env_name": "virtualenv-py3.8-numpy-pandas", "date": 1655662291000, "params": {"arch": "x86_64", "cpu": "AMD Ryzen 7 4800H with Radeon Graphics", "machine": "tuxpulse", "num_cpu": "16", "os": "Linux 5.15.0-10033-tuxedo", "ram": "16GB", "python": "3.8", "numpy": "", "pandas": ""}, "python": "3.8", "requirements": {"numpy": "", "pandas": ""}, "env_vars": {}, "result_columns": ["result", "params", "version", "started_at", "duration", "stats_ci_99_a", "stats_ci_99_b", "stats_q_25", "stats_q_75", "stats_number", "stats_repeat", "samples", "profile"], "results": {"bench_open_dataset.OpenDatasetSuite.time_read": [[0.6675610369984497], [], "d8736c9050c8133f745cc20f6d35fcbac47a834c114c581fff6c8b4123f99d6a", 1655665724254, 8.1799, [0.66404], [0.67107], [0.66708], [0.66929], [1], [10]], "bench_readnek.ReadnekSuite.time_read": [[0.031290766501115286], [], "4247c82d15af70d066d71b05656858fa8f9088d8df24e2be77eb30b987a05a4b", 1655665728370, 0.60922, [0.031064], [0.036323], [0.031117], [0.031526], [1], [10]]}, "durations": {"<build>": 6.579625368118286}, "version": 2}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"commit_hash": "53a66ce515a41d5667d9452082c4da17f6791442", "env_name": "virtualenv-py3.8-dask[array]-numpy-pandas", "date": 1655624617000, "params": {"machine": "tuxpulse", "os": "Linux 5.15.0-10033-tuxedo", "arch": "x86_64", "cpu": "AMD Ryzen 7 4800H with Radeon Graphics", "num_cpu": "16", "ram": "16GB", "python": "3.8", "numpy": "", "dask[array]": "", "pandas": ""}, "python": "3.8", "requirements": {"numpy": "", "dask[array]": "", "pandas": ""}, "env_vars": {}, "result_columns": ["result", "params", "version", "started_at", "duration", "stats_ci_99_a", "stats_ci_99_b", "stats_q_25", "stats_q_75", "stats_number", "stats_repeat", "samples", "profile"], "results": {"bench_open_dataset.OpenDatasetSuite.time_read": [[0.7063027359981788], [], "d8736c9050c8133f745cc20f6d35fcbac47a834c114c581fff6c8b4123f99d6a", 1655662111786, 8.6814, [0.68268], [0.79311], [0.69674], [0.72925], [1], [10]], "bench_readnek.ReadnekSuite.time_read": [[0.037250710500302375], [], "4247c82d15af70d066d71b05656858fa8f9088d8df24e2be77eb30b987a05a4b", 1655662116052, 0.71059, [0.032594], [0.039794], [0.034223], [0.039192], [1], [10]]}, "durations": {}, "version": 2}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"commit_hash": "53a66ce515a41d5667d9452082c4da17f6791442", "env_name": "virtualenv-py3.8-numpy-pandas", "date": 1655624617000, "params": {"machine": "tuxpulse", "os": "Linux 5.15.0-10033-tuxedo", "arch": "x86_64", "cpu": "AMD Ryzen 7 4800H with Radeon Graphics", "num_cpu": "16", "ram": "16GB", "python": "3.8", "numpy": "", "pandas": ""}, "python": "3.8", "requirements": {"numpy": "", "pandas": ""}, "env_vars": {}, "result_columns": ["result", "params", "version", "started_at", "duration", "stats_ci_99_a", "stats_ci_99_b", "stats_q_25", "stats_q_75", "stats_number", "stats_repeat", "samples", "profile"], "results": {"bench_open_dataset.OpenDatasetSuite.time_read": [[0.6838599720013008], [], "d8736c9050c8133f745cc20f6d35fcbac47a834c114c581fff6c8b4123f99d6a", 1655662134561, 8.4276, [0.66956], [0.70753], [0.68118], [0.69425], [1], [10]], "bench_readnek.ReadnekSuite.time_read": [[0.031951281998772174], [], "4247c82d15af70d066d71b05656858fa8f9088d8df24e2be77eb30b987a05a4b", 1655662138775, 0.66082, [0.031676], [0.032222], [0.031877], [0.032062], [1], [10]]}, "durations": {"<build>": 13.125392198562622}, "version": 2}
9 changes: 9 additions & 0 deletions docs/asv_bench/results/tuxpulse/machine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"arch": "x86_64",
"cpu": "AMD Ryzen 7 4800H with Radeon Graphics",
"machine": "tuxpulse",
"num_cpu": "16",
"os": "Linux 5.15.0-10033-tuxedo",
"ram": "16GB",
"version": 1
}
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ interpreter of your choice:

::

conda create -n my-env -c conda-forge pypy pip xarray pydantic
conda create -n my-env -c conda-forge pypy pip xarray
conda activate my-env
pypy -m pip install pymech

Expand Down
6 changes: 6 additions & 0 deletions docs/neksuite.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ readnek() is clever enough to figure out the rest.
The output is a single :py:class:`pymech.core.HexaData` data structure containing all
the information that was stored in the file.

.. note::

If you are only interested in the metadata (for example, the simulation
time) of a solution file a quicker solution would be to execute
:py:func:`pymech.neksuite.field.read_header`

writenek
--------
This function writes a binary ``.f%05d`` file in the same format that *Nek5000*
Expand Down
79 changes: 54 additions & 25 deletions pymech/neksuite/field.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,77 @@
from __future__ import annotations

import os
import io
import struct
import sys
from typing import Optional, Tuple
from pathlib import Path
from typing import Optional, Tuple, Union, BinaryIO

import numpy as np
from pydantic.dataclasses import ValidationError, dataclass

from attrs import define, field

from pymech.core import HexaData
from pymech.log import logger


@dataclass
def _as_unicode(string: StringOrBytes) -> str:
if isinstance(string, bytes):
return string.decode()
else:
return string


def _as_tuple_of_ints(seq):
return tuple(int(s) for s in seq)


StringOrBytes = Union[str, bytes]
PathLike = Union[str, os.PathLike]


@define
class Header:
"""Dataclass for Nek5000 field file header. This relies on the package
pydantic_ and its ability to do type-checking and type-coercion of the
attrs_ and its ability to do type-validation and type-conversion of the
header metadata.
.. _pydantic: https://pydantic-docs.helpmanual.io/
.. _attrs: https://www.attrs.org/en/stable/
"""

# get word size: single or double precision
wdsz: int
wdsz: int = field(converter=int)
# get polynomial order
orders: Tuple[int, ...]
orders: Tuple[int, ...] = field(converter=_as_tuple_of_ints)
# get number of elements
nb_elems: int
nb_elems: int = field(converter=int)
# get number of elements in the file
nb_elems_file: int
nb_elems_file: int = field(converter=int)
# get current time
time: float
time: float = field(converter=float)
# get current time step
istep: int
istep: int = field(converter=int)
# get file id
fid: int
fid: int = field(converter=int)
# get tot number of files
nb_files: int
nb_files: int = field(converter=int)

# get variables [XUPTS[01-99]]
variables: Optional[str] = None
# NOTE: field(factory=...) specifies the default value for the field.
# https://www.attrs.org/en/stable/init.html#defaults

# get variables [XUPTS[01-99]]
variables: str = field(converter=_as_unicode, factory=str)
# floating point precision
realtype: Optional[str] = None

realtype: str = field(factory=str)
# compute total number of points per element
nb_pts_elem: Optional[int] = None
nb_pts_elem: int = field(factory=int)
# get number of physical dimensions
nb_dims: Optional[int] = None
nb_dims: int = field(factory=int)
# get number of variables
nb_vars: Optional[Tuple[int, ...]] = None
nb_vars: Tuple[int, ...] = field(factory=tuple)

def __post_init_post_parse__(self):
def __attrs_post_init__(self):
# get word size: single or double precision
wdsz = self.wdsz
if not self.realtype:
Expand All @@ -70,7 +90,7 @@ def __post_init_post_parse__(self):
self.nb_dims = 2 + int(orders[2] > 1)

if not self.variables and not self.nb_vars:
raise ValidationError("Both variables and nb_vars cannot be None", self)
raise ValueError("Both variables and nb_vars cannot be uninitialized.")
elif self.variables:
self.nb_vars = self._variables_to_nb_vars()
elif self.nb_vars:
Expand Down Expand Up @@ -132,17 +152,26 @@ def as_bytestring(self) -> bytes:
return header.ljust(132).encode("utf-8")


def read_header(fp: io.BufferedReader) -> Header:
def read_header(path_or_file_obj: Union[PathLike, BinaryIO]) -> Header:
"""Make a :class:`pymech.neksuite.Header` instance from a file buffer
opened in binary mode.
"""
header = fp.read(132).split()
if isinstance(path_or_file_obj, (str, os.PathLike)):
with Path(path_or_file_obj).open("rb") as fp:
header = fp.read(132).split()
elif isinstance(path_or_file_obj, io.BufferedReader):
fp = path_or_file_obj
header = fp.read(132).split()
else:
raise ValueError("Should be a path or opened file object in 'rb' mode.")

logger.debug(b"Header: " + b" ".join(header))
if len(header) < 12:
raise IOError("Header of the file was too short.")

return Header(header[1], header[2:5], *header[5:12])
# Relying on attrs converter to type-cast. Mypy will complain
return Header(header[1], header[2:5], *header[5:12]) # type: ignore[arg-type]


# ==============================================================================
Expand Down
9 changes: 5 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ python_requires = >= 3.7
packages = find:
install_requires =
xarray >= 0.19.0, != 0.20.0, != 0.20.1
pydantic >= 1.8.1
importlib_metadata < 5.0.0 ; python_version < "3.8"
attrs >= 21.4.0

setup_requires =
setuptools>=39.2.0
Expand All @@ -47,11 +48,11 @@ docs =
sphinx-copybutton
sphinx-inline-tabs
myst-nb
importlib_metadata; python_version < "3.8"
asv >= 0.5.1

tests =
%(full)s
mypy
pytest >= 6.2.5
pytest-cov >= 3.0.0
pytest-xdist >= 2.4.0
Expand All @@ -69,7 +70,7 @@ xarray.backends =

[flake8]
max-line-length = 88
ignore = W503, E203, E501
extend-ignore = W503, E203, E501

[coverage:run]
source = pymech
Expand Down Expand Up @@ -97,4 +98,4 @@ ignore_errors = True
addopts = "--cov=pymech"

[mypy]
plugins = pydantic.mypy
ignore_missing_imports = True
14 changes: 14 additions & 0 deletions tests/test_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest

from pymech.neksuite.field import Header, read_header


def test_header_var_nb_vars_uninitialized():
with pytest.raises(ValueError):
Header(4, [8, 8, 8], 2, 2, 100, 10000, 3, 1)


def test_header_time(test_data_dir):
fname = f"{test_data_dir}/nek/channel3D_0.f00001"
header = read_header(fname)
assert header.time == 0.2

0 comments on commit b11e076

Please sign in to comment.