Utils

unimol_tools.utils contains the utils related to the model, such as metrics and logger.

Metrics

unimol_tools.utils.metrics contains the metrics included in the model.

class unimol_tools.utils.metrics.Metrics(task=None, metrics_str=None, **params)[source]

Class for calculating metrics for different tasks.

Parameters:
  • task – The task type. Supported tasks are ‘regression’, ‘multilabel_regression’, ‘classification’, ‘multilabel_classification’, and ‘multiclass’.

  • metrics_str – Comma-separated string of metric names. If provided, only the specified metrics will be calculated. If not provided or an empty string, default metrics for the task will be used.

__init__(task=None, metrics_str=None, **params)[source]
cal_classification_metric(label, predict, nan_value=-1.0, threshold=None)[source]
Parameters:
  • label – the labels of the dataset.

  • predict – the predict values of the model.

cal_reg_metric(label, predict, nan_value=-1.0)[source]
Parameters:
  • label – the labels of the dataset.

  • predict – the predict values of the model.

cal_multiclass_metric(label, predict, nan_value=-1.0, label_cnt=-1)[source]
Parameters:
  • label – the labels of the dataset.

  • predict – the predict values of the model.

Logger

unimol_tools.utils.base_logger.py control the logger.

class unimol_tools.utils.base_logger.PackagePathFilter(name='')[source]

A custom logging filter for adding the relative path to the log record.

filter(record)[source]

add relative path to record

class unimol_tools.utils.base_logger.Logger(logger_name='None')[source]

A custom logger class that provides logging functionality to console and file.

__init__(logger_name='None')[source]
Parameters:

logger_name – (str) The name of the logger (default: ‘None’)

get_logger()[source]

Get the logger object.

Returns:

logging.Logger - a logger object.

Config

unimol_tools.utils.config_handler.py manages the config input file.

class unimol_tools.utils.config_handler.YamlHandler(file_path)[source]

A clss to read and write the yaml file

__init__(file_path)[source]

A custom logger class that provides logging functionality to console and file.

Parameters:

file_path – (str) The yaml file path of the config.

read_yaml(encoding='utf-8')[source]

read yaml file and convert to easydict

Parameters:

encoding – (str) encoding method uses utf-8 by default

Returns:

Dict (addict), the usage of Dict is the same as dict

write_yaml(data, out_file_path, encoding='utf-8')[source]

write dict or easydict to yaml file(auto write to self.file_path)

Parameters:

data – (dict or Dict(addict)) dict containing the contents of the yaml file

unimol_tools.utils.config_handler.addict2dict(addict_obj)[source]

convert addict to dict

Parameters:

addict_obj – (Dict(addict)) the addict obj that you want to convert to dict

Returns:

(Dict) converted result

Padding

unimol_tools.utils.util.py contain some padding methods.

unimol_tools.utils.util.pad_1d_tokens(values, pad_idx, left_pad=False, pad_to_length=None, pad_to_multiple=1)[source]

padding one dimension tokens inputs.

Parameters:
  • values – A list of 1d tensors.

  • pad_idx – The padding index.

  • left_pad – Whether to left pad the tensors. Defaults to False.

  • pad_to_length – The desired length of the padded tensors. Defaults to None.

  • pad_to_multiple – The multiple to pad the tensors to. Defaults to 1.

Returns:

A padded 1d tensor as a torch.Tensor.

unimol_tools.utils.util.pad_2d(values, pad_idx, dim=1, left_pad=False, pad_to_length=None, pad_to_multiple=1)[source]

padding two dimension tensor inputs.

Parameters:
  • values – A list of 2d tensors.

  • pad_idx – The padding index.

  • left_pad – Whether to pad on the left side. Defaults to False.

  • pad_to_length – The length to pad the tensors to. If None, the maximum length in the list is used. Defaults to None.

  • pad_to_multiple – The multiple to pad the tensors to. Defaults to 1.

Returns:

A padded 2d tensor as a torch.Tensor.

unimol_tools.utils.util.pad_coords(values, pad_idx, dim=3, left_pad=False, pad_to_length=None, pad_to_multiple=1)[source]

padding two dimension tensor coords which the third dimension is 3.

Parameters:
  • values – A list of 1d tensors.

  • pad_idx – The value used for padding.

  • left_pad – Whether to pad on the left side. Defaults to False.

  • pad_to_length – The desired length of the padded tensor. Defaults to None.

  • pad_to_multiple – The multiple to pad the tensor to. Defaults to 1.

Returns:

A padded 2d coordinate tensor as a torch.Tensor.