Skip to content

Commit eb5d573

Browse files
authored
Merge pull request #637 from FAIRmat-NFDI/convert_int_to_float
convert int-like values silently into float-like for NX_FLOAT
2 parents fcabc91 + 215779d commit eb5d573

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/pynxtools/dataconverter/helpers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,30 @@ def convert_str_to_bool_safe(value: str) -> Optional[bool]:
658658
raise ValueError(f"Could not interpret string '{value}' as boolean.")
659659

660660

661+
def convert_int_to_float(value):
662+
"""
663+
Converts int-like values to float, including values in arrays, and lists
664+
665+
Args:
666+
value: The input value, which can be a single value, list, or numpy array.
667+
668+
Returns:
669+
The input value with all int-like values converted to float.
670+
"""
671+
if isinstance(value, int):
672+
return float(value)
673+
elif isinstance(value, list):
674+
return [convert_int_to_float(v) for v in value]
675+
elif isinstance(value, tuple):
676+
return tuple(convert_int_to_float(v) for v in value)
677+
elif isinstance(value, set):
678+
return {convert_int_to_float(v) for v in value}
679+
elif isinstance(value, np.ndarray) and np.issubdtype(value.dtype, np.integer):
680+
return value.astype(float)
681+
else:
682+
return value
683+
684+
661685
def is_valid_data_field(
662686
value: Any, nxdl_type: str, nxdl_enum: list, nxdl_enum_open: bool, path: str
663687
) -> Any:
@@ -683,6 +707,12 @@ def is_valid_data_field(
683707
collector.collect_and_log(
684708
path, ValidationProblem.InvalidType, accepted_types, nxdl_type
685709
)
710+
elif accepted_types[0] is float:
711+
value = convert_int_to_float(value)
712+
if not is_valid_data_type(value, accepted_types):
713+
collector.collect_and_log(
714+
path, ValidationProblem.InvalidType, accepted_types, nxdl_type
715+
)
686716
else:
687717
collector.collect_and_log(
688718
path, ValidationProblem.InvalidType, accepted_types, nxdl_type

tests/dataconverter/test_validation.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,19 @@ def listify_template(data_dict: Template):
362362
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value",
363363
0,
364364
),
365+
[],
366+
id="int-instead-of-float",
367+
),
368+
pytest.param(
369+
alter_dict(
370+
TEMPLATE,
371+
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value",
372+
np.complex128(0),
373+
),
365374
[
366375
"The value at /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value should be one of the following Python types: (<class 'float'>, <class 'numpy.floating'>), as defined in the NXDL as NX_FLOAT."
367376
],
368-
id="int-instead-of-float",
377+
id="complex-instead-of-float",
369378
),
370379
pytest.param(
371380
alter_dict(
@@ -534,13 +543,18 @@ def listify_template(data_dict: Template):
534543
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value",
535544
[2], # pylint: disable=E1126
536545
),
537-
[
538-
"The value at /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value should be "
539-
"one of the following Python types: (<class 'float'>, <class 'numpy.floating'>), as defined in the NXDL "
540-
"as NX_FLOAT."
541-
],
546+
[],
542547
id="list-of-int-instead-of-float",
543548
),
549+
pytest.param(
550+
alter_dict(
551+
TEMPLATE,
552+
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value",
553+
np.array([2]), # pylint: disable=E1126
554+
),
555+
[],
556+
id="array-of-int-instead-of-float",
557+
),
544558
pytest.param(
545559
set_to_none_in_dict(
546560
TEMPLATE,

0 commit comments

Comments
 (0)