neural_de.utils package

Submodules

neural_de.utils.math module

Collection of utility math-based utility methods

neural_de.utils.math.crop_image(image, ratio)[source]

Return cropped image depends on a ratio of its size Ratio = .3 => crop 30% of image size

Parameters:
  • image (ndarray) – original image

  • ratio (float) – Value in rage [0., 1.[

Return type:

ndarray

Returns:

Cropped image from its center

neural_de.utils.math.get_pad_value(dim, ratio)[source]

Return the number of pixel to add to dim so that it will be a multiple of ratio

Parameters:
  • dim (int) – Actual number of pixel in a dimension

  • ratio (int) – Value dim should be a multiple of

Return type:

int

Returns:

Number of pixel to pad

neural_de.utils.math.is_scaled(image)[source]

Checks to see whether the pixel values have already been rescaled to [0, 1].

Parameters:

image (ndarray) – image batch

Return type:

bool

Returns:

True if image_batch is already scaled

neural_de.utils.model_manager module

This module implements tools functions to manage external models used within the component

class neural_de.utils.model_manager.ModelManager(enhancer, required_model, logger)[source]

Bases: object

Manages all external models required by the library

download_model()[source]

Download weights for an enhancer if they are not already available locally. Weights will be stored at ~/.neuralde/{enhancer_name}/model.pth

Return type:

None

neural_de.utils.twe_logger module

This module provides a logger, adapted from the original Confiance logger, for logging messages with specified formatting and output control. It can log messages to the standard output, to a specified file, or both.

Usage:

Import the module, then get the default logger:

. code-block:: python

import twe_logger logger = twe_logger.get_logger()

If you need a logger with different parameters, call get_logger(.) with the desired parameters:

. code-block:: python

logger = twe_logger.get_logger(filename=”my_logs.log”) logger = twe_logger.get_logger( name=”my_logger”, level=’debug’,filename=’my_logs.log’, output=”both”)

Then, use the logger within your code:

. code-block:: python

logger.info(β€œThis is an info message”) logger.error(β€œThis is an error message”)

neural_de.utils.twe_logger.LOGGER_DEFAULT_NAME

Default name for the neural_de logger

neural_de.utils.twe_logger.LOG_LEVEL

Logging level for all the methods. Options are "info", "debug", β€œwarning”, and ``"critical"

neural_de.utils.twe_logger.LOG_OUTPUT

Logging target. Should be "stdout", "file", or "both". If 'file' or 'both', LOG_FILE will be used as the destination file.

neural_de.utils.twe_logger.LOG_FILE

Name of the file where the logger should write.

neural_de.utils.twe_logger.get_logger(name='neural_de_logger', level='debug', filename='log.csv', output='stdout')[source]

Creates and returns a logger.

Parameters:
  • name (str) – Optional, the name of the logger.

  • level (Union[int, str]) – Optional, the logging level.

  • filename (str) – Optional, name of the file where the logger should write.

  • output (str) – Optional, where should the logger write. Can be stdout, file, or both.

Return type:

Logger

Returns:

The logger.

neural_de.utils.twe_logger.log_and_raise(logger, exception, content)[source]

Log an error message then raising an exception with the same message.

Parameters:
  • logger (Logger) – Logger instance.

  • exception (type[Exception]) – The exception we want to raise.

  • content (str) – Text content to log and add to the raised exception.

Raises:

exception – Raises the provided Exception with the provided message

Return type:

None

neural_de.utils.twe_logger.log_str_to_level(str_level)[source]

Converts a string to a corresponding logging level.

Parameters:

str_level (str) – The logging level as a string.

Return type:

int

Returns:

The corresponding logging level.

neural_de.utils.validation module

This module contains a collection of utility functions to verify that the inputs of the transformation methods are valid.

neural_de.utils.validation.is_batch_valid(image_batch, same_dim=False)[source]

Check if images is a valid batch of image: either a list of array numpy of dimension 3 (h,w, c), or a single numpy array of dimension 4 (batch_size, h, w, c).

Parameters:
  • image_batch (Union[list, ndarray]) – Input to check.

  • same_dim – If True, will also verify that every image has the same dimension

Returns:

The bool is True if the input is a valid batch of images, and

False otherwise. The string is empty if the bool is True, if not it contains the reason of the non validation.

Return type:

Tuple of (bool,string)

neural_de.utils.validation.is_device_valid(device)[source]

Verify if device is a valid torch device.

Parameters:

device (str) – the device we want to check the validity

Return type:

bool

Returns:

True if the input string is a valid torch device, false if not.

neural_de.utils.validation.is_power_of_two(value)[source]

Check if the integer value is a power of 2.

Parameters:

value (int) – Integer to check

Return type:

bool

Returns:

True if value is a power of 2, else otherwise.

Module contents

Modules with the different utility methods of the library :

  • logging

  • model download and checking

  • input validation

  • mathematical operations