Python implementation of the Advanced Scientific Data Format (ASDF) Standard
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Global and context-specific configuration for controlling ASDF behavior including validation settings, version defaults, array handling preferences, and I/O performance tuning.
Access and manage the global ASDF configuration that affects all operations.
def get_config():
"""
Get the current global ASDF configuration.
Returns:
AsdfConfig: Current configuration object
"""Temporarily modify configuration settings within a specific scope without affecting global settings.
def config_context():
"""
Context manager for temporary configuration changes.
Returns:
AsdfConfig: Configuration object that can be modified within context
Example:
with asdf.config_context() as config:
config.validate_on_read = False
# Operations here use modified config
# Global config restored after context
"""Configuration object containing all ASDF behavioral settings with properties for validation, performance, and compatibility control.
class AsdfConfig:
@property
def validate_on_read(self) -> bool:
"""
Whether to validate ASDF files when reading.
Default: True
"""
@validate_on_read.setter
def validate_on_read(self, value: bool):
"""Set validation on read behavior."""
@property
def default_version(self) -> str:
"""
Default ASDF version for new files.
Default: Latest supported version
"""
@default_version.setter
def default_version(self, value: str):
"""Set default ASDF version."""
@property
def io_block_size(self) -> int:
"""
Block size for file I/O operations in bytes.
Default: 65536 (64KB)
"""
@io_block_size.setter
def io_block_size(self, value: int):
"""Set I/O block size."""
@property
def array_inline_threshold(self) -> int:
"""
Size threshold in bytes for inlining arrays in YAML.
Arrays smaller than this are stored inline, larger ones in binary blocks.
Default: 100
"""
@array_inline_threshold.setter
def array_inline_threshold(self, value: int):
"""Set array inline threshold."""
@property
def all_array_storage(self) -> str:
"""
Global override for array storage mode.
Options: None, 'internal', 'external'
Default: None (use per-array settings)
"""
@all_array_storage.setter
def all_array_storage(self, value: str):
"""Set global array storage mode."""
@property
def all_array_compression(self) -> str:
"""
Global override for array compression.
Options: None, 'none', 'zlib', 'bzp2', 'lz4'
Default: None (use per-array settings)
"""
@all_array_compression.setter
def all_array_compression(self, value: str):
"""Set global array compression."""
@property
def legacy_fill_schema_defaults(self) -> bool:
"""
Whether to fill in schema defaults for legacy files.
Default: True
"""
@legacy_fill_schema_defaults.setter
def legacy_fill_schema_defaults(self, value: bool):
"""Set legacy schema defaults behavior."""
@property
def all_array_compression_kwargs(self) -> dict:
"""
Global compression keyword arguments for all arrays.
Dictionary of kwargs passed to compressor during block compression.
Default: None (no keyword arguments)
"""
@all_array_compression_kwargs.setter
def all_array_compression_kwargs(self, value: dict):
"""Set global array compression keyword arguments."""
@property
def default_array_save_base(self) -> bool:
"""
Whether to save base arrays for views by default.
When True, views of the same array refer to offsets/strides of same block.
Default: True
"""
@default_array_save_base.setter
def default_array_save_base(self, value: bool):
"""Set default array save base behavior."""
@property
def convert_unknown_ndarray_subclasses(self) -> bool:
"""
Whether to convert unknown ndarray subclasses to ndarray.
When True, unhandled ndarray subclasses appear as regular ndarrays.
Default: False (deprecated feature, issues AsdfConversionWarning)
"""
@convert_unknown_ndarray_subclasses.setter
def convert_unknown_ndarray_subclasses(self, value: bool):
"""Set ndarray subclass conversion behavior."""
@property
def lazy_tree(self) -> bool:
"""
Whether ASDF tree contents are lazily converted to custom objects.
When True, custom objects created only when accessed.
Default: False
"""
@lazy_tree.setter
def lazy_tree(self, value: bool):
"""Set lazy tree conversion behavior."""
# Read-only properties
@property
def resource_mappings(self) -> list:
"""
List of registered resource mapping instances.
Loaded from entry points unless overridden by user configuration.
"""
@property
def resource_manager(self):
"""
ResourceManager instance including resources from registered mappings.
"""
@property
def extensions(self) -> list:
"""
List of registered extensions loaded from entry points.
"""import asdf
# Get current configuration
config = asdf.get_config()
print(f"Validation on read: {config.validate_on_read}")
print(f"Default version: {config.default_version}")
print(f"Array inline threshold: {config.array_inline_threshold} bytes")
print(f"I/O block size: {config.io_block_size} bytes")# Use context manager for temporary changes
with asdf.config_context() as config:
# Disable validation for performance
config.validate_on_read = False
# Use older version for compatibility
config.default_version = "1.4.0"
# Operations within this block use modified settings
af = asdf.AsdfFile({"data": [1, 2, 3]})
af.write_to("output.asdf") # Uses version 1.4.0, no validation
# Outside context, original settings restored
print(asdf.get_config().default_version) # Back to original# Configure for large file operations
with asdf.config_context() as config:
# Increase I/O block size for better throughput
config.io_block_size = 1024 * 1024 # 1MB blocks
# Increase inline threshold to reduce small array overhead
config.array_inline_threshold = 1000
# Disable validation for faster writes
config.validate_on_read = False
# Process large dataset
process_large_dataset()# Set global compression for all arrays
with asdf.config_context() as config:
config.all_array_compression = 'lz4'
# All arrays will use LZ4 compression
data = {
"array1": np.random.random(10000),
"array2": np.arange(50000),
"array3": np.zeros((100, 100))
}
af = asdf.AsdfFile(data)
af.write_to("compressed.asdf")# Configure for legacy file compatibility
with asdf.config_context() as config:
# Enable legacy schema defaults
config.legacy_fill_schema_defaults = True
# Use older version for maximum compatibility
config.default_version = "1.3.0"
# Read old ASDF files with better compatibility
af = asdf.open("legacy_file.asdf")# Configuration for development/testing
def test_mode_config():
with asdf.config_context() as config:
# Strict validation for testing
config.validate_on_read = True
# Use latest version for testing new features
config.default_version = "1.5.0"
# Small inline threshold to test binary block handling
config.array_inline_threshold = 10
yield config
# Use in tests
with test_mode_config():
# Run tests with strict settings
run_validation_tests()# Configure for memory-constrained environments
with asdf.config_context() as config:
# Smaller I/O blocks to reduce memory usage
config.io_block_size = 8192 # 8KB
# Lower inline threshold to move more data to disk
config.array_inline_threshold = 50
# Enable compression to reduce memory footprint
config.all_array_compression = 'zlib'
# Process data with reduced memory usage
process_memory_efficiently()def configure_for_environment(env_type):
"""Configure ASDF for different deployment environments."""
config = asdf.get_config()
if env_type == "production":
config.validate_on_read = True # Safety first
config.all_array_compression = 'lz4' # Good compression/speed balance
config.io_block_size = 262144 # 256KB for network efficiency
elif env_type == "development":
config.validate_on_read = True # Catch errors early
config.array_inline_threshold = 10 # Test binary handling
elif env_type == "performance":
config.validate_on_read = False # Maximum speed
config.io_block_size = 1048576 # 1MB blocks
config.all_array_compression = 'none' # No compression overhead
# Apply environment-specific configuration
configure_for_environment("production")Install with Tessl CLI
npx tessl i tessl/pypi-asdf