molop.config¶
本页面提供了 molop.config 模块的 API 参考。
Author: TMJ Date: 2025-01-15 23:01:22 LastEditors: TMJ LastEditTime: 2025-12-10 23:37:08 Description: 请填写简介
MolOPConfig
¶
Bases: BaseModel
Configuration class for MolOP operations. Used to manage settings related to molecule processing, fingerprint generation, and logging.
Source code in src/molop/config/__init__.py
class MolOPConfig(BaseModel):
"""
Configuration class for MolOP operations.
Used to manage settings related to molecule processing, fingerprint generation, and logging.
"""
model_config = ConfigDict(arbitrary_types_allowed=True)
# --- General Settings ---
show_progress_bar: bool = Field(default=True, description="Whether to display the progress bar")
max_jobs: int = Field(default=16, description="Maximum number of parallel jobs")
# --- Fingerprint Generator Settings ---
morgan_fpgen: FingerprintGenerator64 = Field(
default=GetMorganGenerator(radius=3, fpSize=1024, includeChirality=True),
description="Morgan fingerprint generator",
)
atompair_fpgen: FingerprintGenerator64 = Field(
default=GetAtomPairGenerator(fpSize=1024, includeChirality=True),
description="AtomPair fingerprint generator",
)
rdkit_fpgen: FingerprintGenerator64 = Field(
default=GetRDKitFPGenerator(fpSize=1024),
description="RDKit fingerprint generator",
)
topological_torsion_fpgen: FingerprintGenerator64 = Field(
default=GetTopologicalTorsionGenerator(fpSize=1024),
description="TopologicalTorsion fingerprint generator",
)
# --- Advanced Settings ---
strict_structure_recovery: bool = Field(
default=False, description="Whether to perform strict structure recovery"
)
max_structure_recovery_time: float = Field(
default=10.0, description="Maximum structure recovery time (seconds)"
)
allow_spin_change: bool = Field(default=False, description="Whether to allow spin changes")
force_unit_transform: bool = Field(
default=False, description="Whether to force unit conversion"
)
parallel_max_size: int = Field(
default=8 * 1024**2,
description="Maximum data size for parallel processing (bytes)",
)
max_recursion_depth: int = Field(default=3000, description="Maximum recursion depth for Python")
# --- Log File Control ---
log_to_file: bool = Field(default=False, description="Whether to write log messages to a file")
# --- DOF Effect Drawer Control ---
use_dof_effect_drawer: bool = Field(
default=True, description="Whether to use DOF effect drawer"
)
def __init__(self, **data):
"""
Initializes the configuration object and configures the logger based on current settings.
"""
super().__init__(**data)
sys.setrecursionlimit(self.max_recursion_depth)
# Set log state based on initial configuration values
if self.show_progress_bar:
self.verbose()
else:
self.quiet()
if self.log_to_file:
self.enable_file_logging()
else:
self.disable_file_logging()
if self.use_dof_effect_drawer:
self.set_dof_effect_drawer(enable=True)
else:
self.set_dof_effect_drawer(enable=False)
def quiet(self):
"""
Disables the progress bar and console log output.
This allows the program to run silently in the background.
"""
self.show_progress_bar = False
if stream_handler in moloplogger.handlers:
moloplogger.removeHandler(stream_handler)
def verbose(self):
"""
Enables the progress bar and console log output.
"""
self.show_progress_bar = True
if stream_handler not in moloplogger.handlers:
moloplogger.addHandler(stream_handler)
def enable_file_logging(self):
"""Enable logging to a file."""
self.log_to_file = True
if file_handler not in moloplogger.handlers:
moloplogger.addHandler(file_handler)
logging.info(
f"File logging enabled. Logs will be written to: {getattr(file_handler, 'baseFilename', 'N/A')}"
)
def disable_file_logging(self):
"""Disable logging to a file."""
self.log_to_file = False
if file_handler in moloplogger.handlers:
moloplogger.removeHandler(file_handler)
def set_log_level(self, level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]):
"""
Sets the level for the molop logger.
Args:
level (str): The logging level, must be one of 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'.
"""
try:
moloplogger.setLevel(level)
logging.info(f"Log level set to {level}")
except ValueError as e:
logging.error(f"Error setting log level: Invalid level '{level}'")
raise ValueError(f"Invalid log level: {level}") from e
def set_max_recursion_depth(self, depth: int):
"""
Set the maximum recursion depth for Python.
Args:
depth (int): The maximum recursion depth.
"""
sys.setrecursionlimit(depth)
self.max_recursion_depth = depth
logging.info(f"Maximum recursion depth set to {depth}")
def set_n_jobs(self, n_jobs: int):
return (
min(n_jobs, multiprocessing.cpu_count())
if n_jobs > 0
else min(multiprocessing.cpu_count(), self.max_jobs)
)
def set_dof_effect_drawer(self, enable: bool):
"""
Set whether to use the DOF effect drawer.
Args:
enable (bool): Whether to use the DOF effect drawer.
"""
dofconfig.enable_ipython_integration(enable)
__init__(**data)
¶
Initializes the configuration object and configures the logger based on current settings.
Source code in src/molop/config/__init__.py
def __init__(self, **data):
"""
Initializes the configuration object and configures the logger based on current settings.
"""
super().__init__(**data)
sys.setrecursionlimit(self.max_recursion_depth)
# Set log state based on initial configuration values
if self.show_progress_bar:
self.verbose()
else:
self.quiet()
if self.log_to_file:
self.enable_file_logging()
else:
self.disable_file_logging()
if self.use_dof_effect_drawer:
self.set_dof_effect_drawer(enable=True)
else:
self.set_dof_effect_drawer(enable=False)
disable_file_logging()
¶
enable_file_logging()
¶
Enable logging to a file.
Source code in src/molop/config/__init__.py
quiet()
¶
Disables the progress bar and console log output. This allows the program to run silently in the background.
set_dof_effect_drawer(enable)
¶
Set whether to use the DOF effect drawer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enable
|
bool
|
Whether to use the DOF effect drawer. |
required |
set_log_level(level)
¶
Sets the level for the molop logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
The logging level, must be one of ‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’. |
required |
Source code in src/molop/config/__init__.py
def set_log_level(self, level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]):
"""
Sets the level for the molop logger.
Args:
level (str): The logging level, must be one of 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'.
"""
try:
moloplogger.setLevel(level)
logging.info(f"Log level set to {level}")
except ValueError as e:
logging.error(f"Error setting log level: Invalid level '{level}'")
raise ValueError(f"Invalid log level: {level}") from e
set_max_recursion_depth(depth)
¶
Set the maximum recursion depth for Python.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
int
|
The maximum recursion depth. |
required |