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

Except unit uni pint quantity. #527

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
13 changes: 12 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ distlib==0.3.9
# via virtualenv
filelock==3.16.1
# via virtualenv
flexcache==0.3
# via pint
flexparser==0.4
# via pint
fonttools==4.55.2
# via matplotlib
ghp-import==2.1.0
Expand Down Expand Up @@ -135,9 +139,12 @@ pathspec==0.12.1
# mkdocs-macros-plugin
pillow==11.0.0
# via matplotlib
pint==0.24.4
# via pynxtools (pyproject.toml)
platformdirs==4.3.6
# via
# mkdocs-get-deps
# pint
# virtualenv
pluggy==1.5.0
# via pytest
Expand Down Expand Up @@ -204,7 +211,11 @@ types-pyyaml==6.0.12.20240917
types-requests==2.32.0.20241016
# via pynxtools (pyproject.toml)
typing-extensions==4.12.2
# via mypy
# via
# flexcache
# flexparser
# mypy
# pint
tzdata==2024.2
# via pandas
urllib3==2.2.3
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies = [
"importlib-metadata",
"lxml>=4.9.1",
"anytree",
"pint>=0.17",
]

[project.urls]
Expand Down
22 changes: 18 additions & 4 deletions src/pynxtools/dataconverter/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

import copy
import logging
import sys
import xml.etree.ElementTree as ET

import h5py
import numpy as np
import pint

from pynxtools.dataconverter import helpers
from pynxtools.dataconverter.exceptions import InvalidDictProvided
Expand All @@ -35,7 +35,7 @@
)

logger = logging.getLogger("pynxtools") # pylint: disable=C0103

ureg = pint.UnitRegistry()

def does_path_exist(path, h5py_obj) -> bool:
"""Returns true if the requested path exists in the given h5py object."""
Expand Down Expand Up @@ -259,8 +259,22 @@ def _put_data_into_hdf5(self):

def add_units_key(dataset, path):
units_key = f"{path}/@units"
if units_key in self.data.keys() and self.data[units_key] is not None:
dataset.attrs["units"] = self.data[units_key]
units = self.data.get(units_key, None)
if units is None:
return
if isinstance(units, pint.Unit):
units = str(units)
else:
try:
ureg.Unit(units)
except pint.errors.UndefinedUnitError as exc:
massage = (
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
massage = (
message = (

f"Units provided for path: '{path}@units' are not valid."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
f"Units provided for path: '{path}@units' are not valid."
f"Units '{units}' provided for path: '{path}@units' are not valid."

f" Please provide a valid unit."
)
raise InvalidDictProvided(massage) from exc
Copy link
Collaborator

@lukaspie lukaspie Jan 31, 2025

Choose a reason for hiding this comment

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

Shall this raise an error or simply be logged as a warning? The file can be written without this wrong unit, right?

Suggested change
raise InvalidDictProvided(massage) from exc
logger.warning(message, exc_info=True)


dataset.attrs["units"] = units

for path, value in self.data.items():
try:
Expand Down
Loading