Git Product home page Git Product logo

Comments (8)

aestrivex avatar aestrivex commented on July 22, 2024

from bctpy.

clbarnes avatar clbarnes commented on July 22, 2024

it's probably not desirable for most users if bctpy behaves deterministically by default

Agreed. I was thinking something like

import numpy as np

def randmio_dir(adj, itr, seed=None):
    rng = np.random if seed is None else np.random.RandomState(seed)
    # the rest of the function, using rng

The path of least resistance may be to use the patch strategy above in a decorator:

from unittest import mock
from functools import wraps

def seedable(fn):
    @wraps(fn)
    def wrapped(*args, seed=None, **kwargs):
        if seed is None:
            return fn(*args, **kwargs)
        else:
            with mock.patch('numpy.random', np.random.RandomState(seed)):
                return fn(*args, **kwargs)

@seedable
def randmio_dir(adj, itr):
    """decorated version now has a ``seed`` kwarg"""
    # the rest of the function with no changes

It's worth noting that (annoyingly) RandomState doesn't have exactly the same functions available as np.random - it's notably missing the random/ranf/sample aliases for random_sample.

from bctpy.

aestrivex avatar aestrivex commented on July 22, 2024

from bctpy.

clbarnes avatar clbarnes commented on July 22, 2024

Not a problem! Just wanted to make sure it fit with your design goals before starting on it myself. For my own documentation, another approach to the decorator would be:

from functools import wraps

def seedable(fn):
    @wraps(fn)
    def wrapped(*args, seed=None, **kwargs):
        if seed is None:
            return fn(*args, **kwargs)
        else:
            state = np.random.get_state()
            np.random.seed()
            fn(*args, **kwargs)
            np.random.set_state(state)

Feels less hacky because we're not patching over stuff, but would probably break in threaded contexts. Then again, so might the patch.

from bctpy.

clbarnes avatar clbarnes commented on July 22, 2024

Confirmed, the patch approach also breaks threaded usage. Looks like the decorator is a no-go. Probably better for readability anyway.

from bctpy.

clbarnes avatar clbarnes commented on July 22, 2024

I think I've caught all of the cases where one randomising bct function calls another randomising bct function, and pass a deterministic seed through, but if any of them are calling out to other functions which use np.random under the hood I'm unlikely to have found them.

from bctpy.

aestrivex avatar aestrivex commented on July 22, 2024

Fixed in #70

from bctpy.

clbarnes avatar clbarnes commented on July 22, 2024

Thanks!

Since this was initially raised, numpy updates have deprecated this RandomState API in favour of the Generator API. Fortunately the design (i.e. the get_rng function) should make it easy to drop in the new RNG in without much disruption. I'll raise another issue for that though.

from bctpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.