Skip to content

Commit

Permalink
handle None as metadata. More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
georgievgeorgi committed Nov 8, 2024
1 parent 107e3d8 commit 8c79315
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ramanchada2/io/experimental/rc1_parser/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ def cleanMeta(meta):
meta = meta.decode('utf-8')
meta = cleanMeta(meta)
except Exception:
meta = []
meta = {}
return meta
10 changes: 9 additions & 1 deletion src/ramanchada2/misc/types/spectrum/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
StrictInt, float,
datetime.datetime,
List[Any], Dict[str, Any],
StrictStr]
StrictStr, None]


class SpeMetadataFieldModel(PydRootModel):
Expand Down Expand Up @@ -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())

Expand Down
1 change: 1 addition & 0 deletions tests/end_to_end/test_from_cache_or_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
22 changes: 22 additions & 0 deletions tests/spectrum/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 8c79315

Please sign in to comment.