audeering / audobject

Generic Python interface for serializing objects to YAML
https://audeering.github.io/audobject/
Other
1 stars 0 forks source link

Serialize file paths #14

Closed frankenjoe closed 3 years ago

frankenjoe commented 3 years ago

This adds a feature - as simple as it sounds - I have been struggeling with for some time. It will be useful if a user wants to serialize objects that require input from external files (e.g. a file storing model weights etc.). If we would simply store the file path as it is, we get a YAML that is not be portable as it will not find the files once we move it to another location.

As a solution this adds FilePathResolver to properly save and recover files path arguments. Here's what it does:

  1. when the object is serialized convert the original file path to a path relative to the output directory
  2. when the object is re-instantiated expand the file path again with the current input directory

Example from the usage section:

image

codecov[bot] commented 3 years ago

Codecov Report

Merging #14 (068fb81) into master (550c102) will not change coverage. The diff coverage is 100.0%.

Impacted Files Coverage Δ
audobject/__init__.py 100.0% <ø> (ø)
audobject/core/decorator.py 100.0% <100.0%> (ø)
audobject/core/define.py 100.0% <100.0%> (ø)
audobject/core/object.py 100.0% <100.0%> (ø)
audobject/core/parameter.py 100.0% <100.0%> (ø)
audobject/core/resolver.py 100.0% <100.0%> (ø)
audobject/core/utils.py 100.0% <100.0%> (ø)
hagenw commented 3 years ago

Great.

One question: if we store the same object as a YAML file at two different locations the YAML files will be slightly different (as the relative path is different), but when loading them the path will be nicely expanded and the resulting objects will be identical again. Could it ever be a problem that this produces two different YAML files? I don't see an issue here, but I wanted to ask.

frankenjoe commented 3 years ago

Could it ever be a problem that this produces two different YAML files?

I would say it makes sense that the YAML files look different, since also their locations with respect to the referenced file is different.