Skip to content

Utility Methods

grid(shuffle=True, progbar=None, **kwargs)

Generates a grid of settings.

Parameters:

Name Type Description Default
kwargs

the name of parameter is the key while the values represent items to iterate over

{}

Example

from memo import grid

settings = grid(a=[1,2], b=[1, 2], shuffle=False)
expected = [
    {'a': 1, 'b': 1},
    {'a': 1, 'b': 2},
    {'a': 2, 'b': 1},
    {'a': 2, 'b': 2}
]
assert settings == expected
Source code in memo/_grid.py
def grid(shuffle: bool = True, progbar: bool = None, **kwargs):
    """
    Generates a grid of settings.

    Arguments:
        kwargs: the name of parameter is the key while the values represent items to iterate over

    Example

    ```python
    from memo import grid

    settings = grid(a=[1,2], b=[1, 2], shuffle=False)
    expected = [
        {'a': 1, 'b': 1},
        {'a': 1, 'b': 2},
        {'a': 2, 'b': 1},
        {'a': 2, 'b': 2}
    ]
    assert settings == expected
    ```
    """
    settings = [
        dict(zip(kwargs.keys(), d)) for d in it.product(*[v for v in kwargs.values()])
    ]
    if progbar:
        raise DeprecationWarning(
            "`progbar` is deprecated, use a `from memo import Runner` to get a progbar."
        )
    if shuffle:
        random.shuffle(settings)
    return settings

random_grid(n=30, **kwargs)

Generates a random grid settings.

Parameters:

Name Type Description Default
kwargs

the name of parameter is the key while the values represent items to iterate over

{}

Example

from memo import random_grid

settings = random_grid(n=30, a=[1,2], b=[1, 2])
assert len(settings) == 30
Source code in memo/_grid.py
def random_grid(n: int = 30, **kwargs):
    """
    Generates a random grid settings.

    Arguments:
        kwargs: the name of parameter is the key while the values represent items to iterate over

    Example

    ```python
    from memo import random_grid

    settings = random_grid(n=30, a=[1,2], b=[1, 2])
    assert len(settings) == 30
    ```
    """
    return [{k: random.choice(v) for k, v in kwargs.items()} for _ in range(n)]

time_taken(minutes=False, rounding=2)

Adds additional time-based information to output.

Parameters:

Name Type Description Default
minutes bool

log minutes instead of seconds

False
rounding int

number of decimals to round the timing to

2

Examples:


import numpy as np

from memo import memlist, grid, time_taken

data = []


@memlist(data=data)
@time_taken()
def birthday_experiment(class_size, n_sim):
    sims = np.random.randint(1, 365 + 1, (n_sim, class_size))
    sort_sims = np.sort(sims, axis=1)
    n_uniq = (sort_sims[:, 1:] != sort_sims[:, :-1]).sum(axis = 1) + 1
    proba = np.mean(n_uniq != class_size)
    return {"est_proba": proba}


settings = grid(class_size=range(5, 50), n_sim=[100, 10_000, 1_000_000])

for setting in settings:
    birthday_experiment(**setting)
Source code in memo/_util.py
def time_taken(minutes: bool = False, rounding: int = 2):
    """
    Adds additional time-based information to output.

    Arguments:
        minutes: log minutes instead of seconds
        rounding: number of decimals to round the timing to

    Example:

    ```python
    import numpy as np

    from memo import memlist, grid, time_taken

    data = []


    @memlist(data=data)
    @time_taken()
    def birthday_experiment(class_size, n_sim):
        sims = np.random.randint(1, 365 + 1, (n_sim, class_size))
        sort_sims = np.sort(sims, axis=1)
        n_uniq = (sort_sims[:, 1:] != sort_sims[:, :-1]).sum(axis = 1) + 1
        proba = np.mean(n_uniq != class_size)
        return {"est_proba": proba}


    settings = grid(class_size=range(5, 50), n_sim=[100, 10_000, 1_000_000])

    for setting in settings:
        birthday_experiment(**setting)
    ```
    """

    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            tic = time.time()
            result = func(*args, **kwargs)
            toc = time.time()
            time_total = toc - tic
            if minutes:
                time_total = time_total / 60
            result = {**result, "time_taken": round(time_total, rounding)}
            return result

        return wrapper

    return decorator