Source code for ibllib.misc.misc

# library of small functions
import logging

import numpy as np

_logger = logging.getLogger('ibllib')


def _parametrized(dec):
    def layer(*args, **kwargs):
        def repl(f):
            return dec(f, *args, **kwargs)
        return repl
    return layer


[docs]def structarr(names, shape=None, formats=None): if not formats: formats = ['f8'] * len(names) dtyp = np.dtype({'names': names, 'formats': formats}) return np.zeros(shape, dtype=dtyp)
[docs]def logger_config(name=None): import logging import colorlog """ Setup the logging environment """ if not name: log = logging.getLogger() # root logger else: log = logging.getLogger(name) log.setLevel(logging.INFO) format_str = '%(asctime)s.%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s' date_format = '%Y-%m-%d %H:%M:%S' cformat = '%(log_color)s' + format_str colors = {'DEBUG': 'green', 'INFO': 'cyan', 'WARNING': 'bold_yellow', 'ERROR': 'bold_red', 'CRITICAL': 'bold_purple'} formatter = colorlog.ColoredFormatter(cformat, date_format, log_colors=colors) stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) log.addHandler(stream_handler) return log
[docs]def range_str(values: iter) -> str: """ Given a list of integers, returns a terse string expressing the unique values. Example: indices = [0, 1, 2, 3, 4, 7, 8, 11, 15, 20] range_str(indices) >> '0-4, 7-8, 11, 15 & 20' :param values: An iterable of ints :return: """ import logging logging.getLogger('ibllib').warning( 'This function has moved to iblutil.util.range_str') # iblrplate trial_str = '' values = list(set(values)) for i in range(len(values)): if i == 0: trial_str += str(values[i]) elif values[i] - (values[i - 1]) == 1: if i == len(values) - 1 or values[i + 1] - values[i] > 1: trial_str += f'-{values[i]}' else: trial_str += f', {values[i]}' # Replace final comma with an ampersand k = trial_str.rfind(',') if k > -1: trial_str = f'{trial_str[:k]} &{trial_str[k + 1:]}' return trial_str