Source code for allensdk.brain_observatory.data_release_utils.metadata_utils.id_generator
import pathlib
[docs]class FileIDGenerator(object):
"""
A class to generate a unique integer ID for each file in the
data release
"""
def __init__(self):
self._id_lookup = dict()
self._next_id = 0
self._dummy_value = -999
@property
def dummy_value(self) -> int:
"""
Value reserved for files that are missing from the
release
"""
return self._dummy_value
[docs] def id_from_path(self,
file_path: pathlib.Path) -> int:
"""
Get the unique ID for a file path. If the file has already
been assigned a unique ID, return that. Otherwise, assign
a unique ID to the file path and return it
"""
if not isinstance(file_path, pathlib.Path):
msg = ("file_path must be a pathlib.Path (this is so "
"we can resolve it into an absolute path). You passed "
f"in a {type(file_path)}")
raise ValueError(msg)
if not file_path.is_file():
msg = f"{file_path} is not a file"
raise ValueError(msg)
if file_path.is_symlink():
msg = f"{file_path} is a symlink; must be an actual path"
raise ValueError(msg)
str_path = str(file_path.resolve().absolute())
if str_path not in self._id_lookup:
self._id_lookup[str_path] = self._next_id
self._next_id += 1
while self._next_id == self.dummy_value:
self._next_id += 1
return self._id_lookup[str_path]