Overview
The Metadata class can be used in order to organize lot’s of data stream files. For example you might have a project generated by the Analyzer4D Software and you want to search through all data files in order to find all streams with a certain compression. You could go through each file and check out if the compression matches your requirement which takes a lot of time. The cache creates a database that caches all the metadata available in the qass.tools.analyzer.buffer_parser.Buffer class. You can then query the cache very fast and programmatically in Python!
Example
In this example we create an instance of the cache and synchronize it with the directory my/directory. We then create a template BufferMetadata object and use it with the cache to query for all buffers with a compression_frq = 8. You can use all properties that are in BufferMetadata.properties. The BufferMetadataCache.get_matching_buffers() method returns a list of Buffer objects that are in this case sorted by their process number (as specified in the query).
from qass.tools.analyzer.buffer_metadata_cache import (
BufferMetadataCache as BMC,
BufferMetadata as BM,
select
)
cache = BMC()
cache.synchronize_directory("my/directory")
results = cache.get_matching_buffers(select(BM).filter(BM.compression_frq==8).order_by(BM.process))
qass.tools.analyzer.buffer_metadata_cache
Classes:
| Name | Description |
|---|---|
BufferMetadata |
This class acts as a template for buffer files. It's properties represent all available metadata of a buffer file. |
BufferMetadataCache |
This class acts as a Cache for Buffer Metadata. It uses a database session with a buffer_metadata table to map |
BufferEnum
Bases: TypeDecorator
Methods:
| Name | Description |
|---|---|
__init__ |
|
process_bind_param |
|
process_result_value |
|
Attributes:
| Name | Type | Description |
|---|---|---|
impl |
|
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
__init__
process_bind_param
BufferMetadata
Bases: __Base
This class acts as a template for buffer files. It's properties represent all available metadata of a buffer file. This class is used internally as a database model and can be instantiated to provide a template for a buffer file by populating desired properties and passing the object to the cache which will in turn create a query based on this object.
Methods:
| Name | Description |
|---|---|
buffer_to_metadata |
Converts a Buffer object to a BufferMetadata database object by copying all the @properties from the Buffer |
filepath |
|
Attributes:
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
directory_path
class-attribute
instance-attribute
properties
class-attribute
instance-attribute
properties = ('id', 'project_id', 'directory_path', 'filename', 'header_size', 'process', 'channel', 'datamode', 'datakind', 'datatype', 'process_time', 'process_date_time', 'db_header_size', 'bytes_per_sample', 'db_count', 'full_blocks', 'db_size', 'db_sample_count', 'frq_bands', 'db_spec_count', 'compression_frq', 'compression_time', 'avg_time', 'avg_frq', 'spec_duration', 'frq_start', 'frq_end', 'frq_per_band', 'sample_count', 'spec_count', 'adc_type', 'bit_resolution', 'fft_log_shift', 'streamno', 'preamp_gain', 'analyzer_version', 'partnumber', 'header_hash')
buffer_to_metadata
staticmethod
Converts a Buffer object to a BufferMetadata database object by copying all the @properties from the Buffer object putting them in the BufferMetadata object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
buffer
|
Buffer
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
buffer_metadata |
BufferMetadata
|
|
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
BufferMetadataCache
This class acts as a Cache for Buffer Metadata. It uses a database session with a buffer_metadata table to map metadata to files on the disk. The cache can be queried a lot faster than manually opening a lot of buffer files.
Methods:
| Name | Description |
|---|---|
__init__ |
|
add_files_to_cache |
Add buffer files to the cache by providing the complete filepaths |
get_buffer_metadata_query |
Converts a |
get_matching_buffers |
Calls get_matching_files and converts the result to Buffer objects |
get_matching_files |
Query the Cache for all files matching the properties that selected by the query object. |
get_matching_metadata |
Query the cache for all BufferMetadata database entries matching |
get_non_synchronized_files |
calculate the difference between the set of files and the set of synchronized files |
remove_files_from_cache |
Remove synchronized files from the cache |
synchronize_directory |
synchronize the buffer files in the given paths with the database matching the regex pattern |
Attributes:
| Name | Type | Description |
|---|---|---|
BufferMetadata |
|
|
Buffer_cls |
|
|
Session |
|
|
engine |
|
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | |
__init__
__init__(db_url='sqlite:///:memory:', Buffer_cls=Buffer)
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
add_files_to_cache
add_files_to_cache(files: Sequence[Path], verbose: int = 0, batch_size: int = 1000, machine_id: Union[str, None] = None, check_synced: bool = True)
Add buffer files to the cache by providing the complete filepaths If a file (determined by filename and directory) is already synchronized it will be skipped
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
Sequence[Path]
|
complete filepaths that are added to the cache. The filepath is used with the Buffer class to open a buffer and extract the header information. |
required |
verbose
|
int
|
verbosity level. 0 = no feedback, 1 = progress bar |
0
|
batch_size
|
int
|
The batch size after which the cache will commit a batch to the database |
1000
|
machine_id
|
Union[str, None]
|
A unique identifier for a different machine |
None
|
check_synced
|
bool
|
Whether to check if the files that are about to be added are already synced to the cache |
True
|
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
get_buffer_metadata_query
Converts a BufferMetadata object to a complete query. Every property of the object will be converted into
SQL and returned as a sqlalchemy.orm.query.FromStatement object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
buffer_metadata
|
BufferMetadata
|
The template BufferMetadata object. |
required |
Returns:
| Type | Description |
|---|---|
FromStatement
|
The sqlalchemy query object |
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
get_matching_buffers
get_matching_buffers(query: Select) -> List[Buffer]
Calls get_matching_files and converts the result to Buffer objects
Returns:
| Type | Description |
|---|---|
list[Buffer]
|
List of Buffer objects |
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
get_matching_files
Query the Cache for all files matching the properties that selected by the query object. The usage of the buffer_metadata, filter_functions and sort_key is deprecated and will be removed in two minor versions. Use the sqlalchemy query parameter instead.
Example
Returns all buffer filepaths with channel = 1, A frequency compression of 4, processes above 100 sorted by the process number
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Select
|
A sqlalchemy select statement specifying the properties of the BufferMetadata objects |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
A list with the paths to the buffer files that match the buffer_metadata |
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
get_matching_metadata
get_matching_metadata(query: Select) -> List[BufferMetadata]
Query the cache for all BufferMetadata database entries matching
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Select
|
A sqlalchemy select statement specifying the properties of the BufferMetadata objects |
required |
Returns:
| Type | Description |
|---|---|
list[BufferMetadata]
|
A list with the matching BufferMetadata objects |
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
get_non_synchronized_files
get_non_synchronized_files(files: Sequence[Path], machine_id: Union[str, None] = None) -> Tuple[List[Path], List[Path]]
calculate the difference between the set of files and the set of synchronized files
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
Sequence[Path]
|
Sequence of files to check whether they are already in the cache |
required |
machine_id
|
Union[str, None]
|
machine identifier to provide extra context for the location of the files |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[Path], list[Path]]
|
The set of files that are not synchronized, and the database entries that exist but the file is not present anymore |
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
remove_files_from_cache
Remove synchronized files from the cache
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
List[Path]
|
complete filepaths that are present in the cache |
required |
verbose
|
verbosity level. 0 = no feedback, 1 = progress bar |
0
|
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
synchronize_directory
synchronize_directory(*paths, sync_subdirectories=True, regex_pattern='^.*[p][0-9]*[c][0-9]{1}[b][0-9]{2}', verbose=1, delete_stale_entries=False, machine_id=None, glob_pattern='*p*c?b*')
synchronize the buffer files in the given paths with the database matching the regex pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
The absolute paths to the directory |
()
|
|
sync_subdirectories
|
When True synchronize all of the subdirectories recursively, defaults to True |
True
|
|
regex_pattern
|
The regex pattern validating the buffer naming format (matched on file.name) |
'^.*[p][0-9]*[c][0-9]{1}[b][0-9]{2}'
|
|
verbose
|
verbosity level. 0 = no feedback, 1 = progress bar |
1
|
|
machine_id
|
An optional identifier for a certain machine to enable synchronization of different platforms |
None
|
|
glob_pattern
|
The pattern forwarded to Path.glob. This pattern acts as a preselection for the files retrieved for the regex pattern |
'*p*c?b*'
|
Source code in src/qass/tools/analyzer/buffer_metadata_cache.py
get_declarative_base
Getter for the declarative Base that is used by the class:
BufferMetadataCache.
Returns:
| Type | Description |
|---|---|
declarative base class
|
|