cellvit.utils package¶
Submodules¶
cellvit.utils.cache_models module¶
cellvit.utils.cache_test_database module¶
cellvit.utils.check_cupy module¶
- cellvit.utils.check_cupy.check_cupy(check_gpu: True, logger: ~logging.Logger | None = <cellvit.utils.logger.NullLogger object>) bool [source]¶
Check if CuPy is installed and working correctly.
- Parameters:
check_gpu (True) – Check if CuPy can access the GPU.
logger (Optional[logging.Logger], optional) – Logger. Defaults to None.
- Raises:
SystemError – No CUDA-capable GPU detected by CuPy.
- Returns:
True if CuPy is installed and working correctly.
- Return type:
bool
cellvit.utils.check_module module¶
- cellvit.utils.check_module.check_module(module_name: str) bool [source]¶
Check if a module is installed in the environment.
- cellvit.utils.check_module.perform_module_check(module_name: str, logger: Logger | None = None) None [source]¶
Check if a module is installed in the environment and log the result.
- Parameters:
module_name (str) – Name of the module to check.
logger (Optional[logging.Logger], optional) – Logger. Defaults to None.
- Raises:
e – ImportError if the module is not installed.
cellvit.utils.check_ray module¶
cellvit.utils.clean_classifier module¶
cellvit.utils.download module¶
cellvit.utils.logger module¶
- class cellvit.utils.logger.ColoredFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]¶
Bases:
Formatter
A custom formatter class that adds color to log messages based on the log level.
- Parameters:
logging.Formatter – The base class for formatting log messages.
- None¶
Example
formatter = ColoredFormatter() log_handler.setFormatter(formatter)
- __init__(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)¶
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.
Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting,
str.format()
({}
) formatting orstring.Template
formatting in your format string.Changed in version 3.2: Added the
style
parameter.
- converter()¶
- localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst)
Convert seconds since the Epoch to a time tuple expressing local time. When ‘seconds’ is not passed in, convert the current time instead.
- default_msec_format = '%s,%03d'¶
- default_time_format = '%Y-%m-%d %H:%M:%S'¶
- format(record)[source]¶
Formats the log record and adds color based on the log level.
- Parameters:
record (logging.LogRecord) – The log record to be formatted.
- Returns:
The formatted log message with added color.
- Return type:
str
Example
log_message = super().format(record) return f’{COLOR_CODES_LOGGING[record.levelname]}{log_message}’
- formatException(ei)¶
Format and return the specified exception information as a string.
This default implementation just uses traceback.print_exception()
- formatMessage(record)¶
- formatStack(stack_info)¶
This method is provided as an extension point for specialized formatting of stack information.
The input data is a string as returned from a call to
traceback.print_stack()
, but with the last trailing newline removed.The base implementation just returns the value passed in.
- formatTime(record, datefmt=None)¶
Return the creation time of the specified LogRecord as formatted text.
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the ‘converter’ attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the ‘converter’ attribute in the Formatter class.
- usesTime()¶
Check if the format uses the creation time of the record.
- class cellvit.utils.logger.Logger(level: Literal['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'], log_dir: Path | str = None, comment: str = 'logs', formatter: str = None, use_timestamp: bool = False, file_level: Literal['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'] = None)[source]¶
Bases:
object
“A Logger for sys-logging and RotatingFileHandler-logging using the python logging module.
Initialize a Logger for sys-logging and RotatingFileHandler-logging by using python logging module. The logger can be used out of the box without any changes, but is also adaptable for specific use cases. In basic configuration, just the log level must be provided. If log_dir is provided, another handler object is created logging into a file into the log_dir directory. The filename can be changes by using comment, which basically is the filename. To create different log files with specific timestamp set ‘use_timestamp’ = True. This adds an additional timestamp to the filename.
- Parameters:
level (Literal["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"]) – Logger.level
log_dir (Union[Path, str], optional) – Path to save logfile in. Defaults to None.
comment (str, optional) – additional comment for save file. Defaults to ‘logs’.
formatter (str, optional) – Custom formatter. Defaults to None.
use_timestamp (bool, optional) – Using timestamp for time-logging. Defaults to False.
file_level (Literal["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"], optional) – Set Logger.level. for output file. Can be useful if a different logging level should be used for terminal output and logging file. If no level is selected, file level logging is the same as for console. Defaults to None.
- level¶
The level of logging.
- Type:
str
- comment¶
The comment to be added to the log file name.
- Type:
str
- log_parent_dir¶
The directory where the log file will be saved.
- Type:
Union[Path, str]
- use_timestamp¶
Whether to use timestamp for time-logging.
- Type:
bool
- formatter¶
The formatter for the log messages.
- Type:
str
- file_level¶
The level of logging for the output file.
- Type:
str
- create_handler(logger
logging.Logger): Creates logging handler for sys output and rotating files in parent_dir.
- Raises:
FileNotFoundError – If the specified log directory does not exist.
Example
>>> logger = Logger(level="INFO", log_dir="/path/to/log_dir", comment="my_logs", use_timestamp=True) >>> logger.create_logger() <Logger __main__ (INFO)>
- __init__(level: Literal['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'], log_dir: Path | str = None, comment: str = 'logs', formatter: str = None, use_timestamp: bool = False, file_level: Literal['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'] = None) None [source]¶