diff --git a/src/ramanchada2/io/experimental/rc1_parser/io.py b/src/ramanchada2/io/experimental/rc1_parser/io.py index 39c16c1c..d26b6086 100644 --- a/src/ramanchada2/io/experimental/rc1_parser/io.py +++ b/src/ramanchada2/io/experimental/rc1_parser/io.py @@ -61,5 +61,5 @@ def cleanMeta(meta): meta = meta.decode('utf-8') meta = cleanMeta(meta) except Exception: - meta = [] + meta = {} return meta diff --git a/src/ramanchada2/misc/types/spectrum/metadata.py b/src/ramanchada2/misc/types/spectrum/metadata.py index 8d81118a..7d9f65a8 100644 --- a/src/ramanchada2/misc/types/spectrum/metadata.py +++ b/src/ramanchada2/misc/types/spectrum/metadata.py @@ -14,7 +14,7 @@ StrictInt, float, datetime.datetime, List[Any], Dict[str, Any], - StrictStr] + StrictStr, None] class SpeMetadataFieldModel(PydRootModel): @@ -54,6 +54,14 @@ def serialize(self): class SpeMetadataModel(PydRootModel): root: Dict[str, SpeMetadataFieldModel] + @field_validator('root', mode='before') + def pre_validate(cls, val): + if val is None or val == '': + val = {} + elif isinstance(val, list): + val = {'%d' % k: v for k, v in enumerate(val)} + return val + def __str__(self): return str(self.serialize()) diff --git a/tests/end_to_end/test_from_cache_or_calc.py b/tests/end_to_end/test_from_cache_or_calc.py index b50994a0..a547044a 100644 --- a/tests/end_to_end/test_from_cache_or_calc.py +++ b/tests/end_to_end/test_from_cache_or_calc.py @@ -47,6 +47,7 @@ def test_from_cache_or_calc(): spe_fit_cache = rc2.spectrum.from_cache_or_calc(cachefile=cachefile, required_steps=steps) assert np.allclose(spe_fit.y, spe_fit_cache.y) # make sure spe_fit_cache is coming from cache + assert spe_fit.meta == spe_fit_cache.meta assert FitPeaksResult.loads(spe_fit.result).to_dataframe_peaks().equals( FitPeaksResult.loads(spe_fit_cache.result).to_dataframe_peaks()) diff --git a/tests/spectrum/test_metadata.py b/tests/spectrum/test_metadata.py index 5d274859..79278874 100644 --- a/tests/spectrum/test_metadata.py +++ b/tests/spectrum/test_metadata.py @@ -51,3 +51,25 @@ def test_spectrum_metadata(): s._flush() assert s.serialize() == {} assert len(s.root) == 0 + + +def test_metadata2(): + assert (SpeMetadataModel.validate(['a0', 'b1', 'c2']) + == SpeMetadataModel.validate({'0': 'a0', '1': 'b1', '2': 'c2'})) + assert SpeMetadataModel.validate(None) == SpeMetadataModel.validate({}) + assert SpeMetadataModel.validate('') == SpeMetadataModel.validate({}) + + meta0 = {'array1': np.array([1, 2, 3, 4, 5, 6]), + 'array2': np.array([1, 2, 3, 4, 5, 6.]), + 'list1': [1, 2, 3, '5', 'asdfasdf'], + 'dict1': {'1': 123, + '2': '234', + '': 2, + '3': None, + '4': [1, 2, 3, 4, 99], + '5': {'a': 'a', '5': 5, '[]': [], '{}': {}}, + }, + b'asdf': 'asdf' + } + meta_ser = SpeMetadataModel(meta0).serialize() + assert SpeMetadataModel(meta0) == SpeMetadataModel(meta_ser)