bamboost.index.base
Indexing of bamboost collections and their simulations/parameters. SQLAlchemy is used to interact with the SQLite database.
The index is generated on the fly or can be explicitly created by scanning the
search_paths for collections. The index is stored as a SQLite database that stores the
path of collections (characterized with a unique UID), as well as the metadata and
parameters of all simulations.
The Index class provides the public API for interacting with the
index. This works in paralell execution, but the class is designed to execute any
operations on the database on the root process only. Methods that return something use
bcast to cast the result to all processes. Any SQL operation is executed only on the
root process!
Database schema:
collections: Contains information about the collections, namely uids and corresponding paths.simulations: Contains information about the simulations, including names, statuses, and links to the corresponding parameters.parameters: Contains the parameters associated with the simulations.
Attributes
- log=
BAMBOOST_LOGGER.getChild('Database') - IDENTIFIER_PREFIX=
'.bamboost-collection' - IDENTIFIER_SEPARATOR=
'-'
Functions
(path, uid) -> dict[str, Any] | NoneLoad the metadata of a collection from its identifier file.
Arguments:
- path:
pathlib.PathPath to the collection directory
- uid:
strUID of the collection
(path, uid) -> NoneCreate an identifier file in the collection directory.
Arguments:
- path:
StrPathPath to the collection directory
- uid:
strUID of the collection
(uid) -> strArguments:
- uid:
str
(func) -> Callable[Concatenate[Index, _P], _T]Decorator to add a session to the function signature.
Arguments:
- func:
typing.Callable[typing_extensions.Concatenate[Index, bamboost._typing._P], bamboost._typing._T]The function to decorate.
(data) -> dict[str, Any]Normalize the metadata of a collection.
This also handles backward compatibility for the "Date of creation" field.
Arguments:
- data:
typing.Mapping[str, typing.Any]The raw metadata dictionary.
(values, *, casefold=False) -> list[str]Deduplicate a sequence of strings.
Arguments:
- values:
typing.AnyThe sequence of strings to deduplicate.
- casefold:
bool=FalseWhether to ignore case when deduplicating.
(value) -> datetime | NoneArguments:
- value:
typing.Any
(path, uid) -> boolArguments:
- path:
pathlib.Path - uid:
str
(path) -> Optional[str]Arguments:
- path:
pathlib.Path
(uid, root_dir) -> tuple[Path, ...]Find the collection with UID under given root_dir.
Arguments:
- uid:
strUID to search for
- root_dir:
pathlib.Pathroot directory for search
(pattern, root_dir, exclude=None) -> Tuple[Path, ...]Locate every file matching pattern under root_dir while pruning directory names listed in exclude (exact-match on the final path part).
Returns an immutable tuple of absolute paths (str) just like the POSIX helper.
Arguments:
- pattern:
str - root_dir:
str | os.os.PathLike - exclude:
typing.Iterable[str] | None=None
(root_dir) -> tuple[tuple[str, Path], ...]Scan the directory for collections.
Arguments:
- root_dir:
pathlib.PathDirectory to scan for collections
Returns
tuple[tuple[str, pathlib.Path], ...]Tuple of tuples with the UID and path of the collectionClasses
CollectionUID
UID of a collection. If no UID is provided, a new one is generated.
(cls, uid=None, length=10)Arguments:
- cls
- uid:
typing.Optional[str]=None - length:
int=10
(length) -> strArguments:
- length:
int
LazyDefaultIndex
(self) -> NoneAttributes:
- _instance=
None
(self, instance) -> NoneArguments:
- instance:
None
(self, instance, value) -> NoneArguments:
- instance:
None - value:
Index
(self, instance, owner) -> IndexArguments:
- instance:
None - owner:
typing.Type[Index]
Index
(self, sql_file=None, comm=None, *, search_paths=None) -> NoneAPI for indexing BAMBOOST collections and simulations.
Arguments:
- search_paths:
typing.Optional[typing.Iterable[str | pathlib.Path]]=None
Attributes:
- _comm=
Communicator() - _engine:
sqlalchemy.Engine - _sm:
typing.Callable[..., sqlalchemy.orm.Session] - _s:
sqlalchemy.orm.Session Paths to scan for collections.
A default index instance. Uses the default SQLite database file and search paths from the configuration.
- _file=
bamboost.index.base.Index(sql_file) or bamboost.config.bamboost.config.index.bamboost.config.index.databaseFileThe path to the SQLite database file.
- _isolated=
bamboost.config.bamboost.config.index.bamboost.config.index.isolatedWhether project based indexing is used.
- _url=
f'sqlite:///{bamboost.index.base.Index(self).bamboost.index.base.Index(self)._file}'The URL to the SQLite database file.
- all_collections:
typing.Sequence[bamboost.index.store.CollectionRecord]Return all collections in the index.
- all_simulations:
typing.Sequence[bamboost.index.store.SimulationRecord]Return all simulations in the index.
- all_parameters:
typing.Sequence[bamboost.index.store.ParameterRecord]Return all parameters in the index.
Usage
Create an instance of the Index class and use its methods to interact
with the index.
$ from bamboost.index import Index
$ index = Index()
Scan for collections in known paths: $ index.scan_for_collections()
Resolve the path of a collection: $ index.resolve_path()
Get a simulation from its collection and simulation name: $ index.get_simulation(, )
(self) -> Generator[Session, None, None]Context manager for a SQL transaction.
If no transaction is active, a new transaction is started. If a transaction is active, the current session is used.
Usage
>>> with index.sql_transaction() as s:
... s.execute(...)(self, *, search_paths=None) -> list[tuple[str, Path]]Scan known paths for collections and update the index.
Iterates through the search paths and searches files with the identifier file structure. If a collection is found, it is added to the cache.
Arguments:
- search_paths:
List[pathlib.Path]=NonePaths to scan for collections. Defaults to config.index.searchPaths.
(self) -> NoneCheck the integrity of the cache.
This method checks if the paths stored in the cache are valid. If a path is not valid, it is removed from the cache.
(self, uid, *, search_paths=None) -> PathResolve and return the path of a collection from its UID. Raises a
FileNotFoundError if the collection is not found in the search paths.
Arguments:
- uid:
strUID of the collection
Paths to search for the collection
(self, path) -> CollectionUIDResolve the UID of a collection from a path.
Returns the UID of the collection or a new UID if it can't be determined.
Arguments:
- path:
StrPathPath of the collection
(self, uid, path=None, *, force_all=False) -> NoneSync the table with the file system.
Iterates through the simulations in the collection and updates the metadata and parameters if the HDF5 file has been modified.
Arguments:
- uid:
strUID of the collection
- path:
typing.Optional=NonePath of the collection
- force_all:
bool=False
(self, uid) -> store.CollectionRecord | NoneReturn a collection from the index.
Arguments:
- uid:
strUID of the collection
(self, collection_uid, name) -> store.SimulationRecord | NoneReturn a simulation from the index.
Arguments:
- collection_uid:
strUID of the collection
- name:
strName of the simulation
(self, uid, path, metadata=None) -> NoneCache a collection in the index.
Arguments:
- uid:
strUID of the collection
- path:
pathlib.PathPath of the collection
- metadata:
typing.Optional[typing.Mapping[str, typing.Any]]=None
(self, collection_uid, simulation_name, parameters=None, metadata=None, *, collection_path=None) -> NoneCache a simulation from a collection.
Arguments:
- collection_uid:
strUID of the collection
- simulation_name:
strName of the simulation
- parameters:
typing.Optional[typing.Mapping[typing.Any, typing.Any]]=None - metadata:
typing.Optional[typing.Mapping[typing.Any, typing.Any]]=None - collection_path:
typing.Optional=NonePath of the collection
(self, collection_uid, simulation_name, data) -> NoneUpdate the metadata of a simulation by passing it as a dict.
Arguments:
- collection_uid:
str - simulation_name:
str - data:
typing.MappingDictionary with new data
(self, collection_uid, simulation_name, parameters) -> NoneUpdate the parameters of a simulation by passing it as a dict.
Arguments:
- collection_uid:
str - simulation_name:
str - parameters:
SimulationParameterTDictionary with new parameters
(self, url) -> NoneArguments:
- url:
str
(self, identifier) -> store.CollectionRecord | NoneArguments:
- identifier:
str
(self, alias) -> str | NoneArguments:
- alias:
str
(self, uid) -> NoneDrop a collection from the cache.
Arguments:
- uid:
strUID of the collection
(self, collection_uid, simulation_name) -> NoneDrop a simulation from the cache.
Arguments:
- collection_uid:
strUID of the collection
- simulation_name:
strName of the simulation
(self, uid) -> Optional[Path]Arguments:
- uid:
str
(self) -> Sequence[store.CollectionRecord](self, collection_uid, simulation_name) -> store.SimulationRecord | NoneArguments:
- collection_uid:
CollectionUID | str - simulation_name:
str
Bamboost