Пример #1
0
class _TestNodes(object):
    C = varnode()
    D = varnode()

    @queuenode
    def queue_output(cls):
        return cls.A() + cls.B()

    @nansumnode
    def nansum_output(cls):
        return cls.A() + cls.sometimes_nan_B()

    @cumprodnode
    def cumprod_output(cls):
        return cls.A() + cls.B()

    @evalnode
    def sometimes_nan_B(cls):
        b = cls.B()
        return np.nan if b % 2 else b

    @evalnode
    def A(cls):
        return cls.C() * cls.D()

    @evalnode
    def B(cls):
        accum = 0

        while True:
            yield accum
            accum += 1

    @queuenode(size=2)
    def queue_size_test(cls):
        return 0

    #
    # check that evalnodes passed as arguments to nodes get
    # bound correctly
    #
    _queue_size = 3

    @evalnode
    def get_queue_size(cls):
        return cls._queue_size

    @queuenode(size=get_queue_size)
    def queue_size_test2(cls):
        return 0

    @evalnode
    def dataframe(cls):
        return pd.DataFrame({"A": range(100)},
                            index=pd.bdate_range(datetime(1970, 1, 1),
                                                 periods=100),
                            dtype=float)

    df_data = datanode(data=dataframe)
    df_filter = filternode(data=dataframe)
Пример #2
0
    def test_dynamic_varnode_pickle(self):
        # shift B by a temporary varnode with a value set in the root context
        temp_varnode = varnode(default="temp_varnode_default")
        shifted_ctx = self.ctx.shift({B : temp_varnode})
        self.ctx[temp_varnode] = "temp_varnode"
        self.assertEquals(shifted_ctx[B], "temp_varnode")

        # pickle the context, unregister the node and recreate the context
        x = pickle.dumps(self.ctx)
        MDFContext.unregister_node(temp_varnode)
        new_ctx = pickle.loads(x)

        # find the shifted context (can't reshift as temp_varnode was unregistered)
        self.assertEquals(len(new_ctx.get_shifted_contexts()), 1)
        shifted_ctx = new_ctx.get_shifted_contexts()[0]

        # check that B in the shifted context is still set
        self.assertEquals(shifted_ctx[B], "temp_varnode")
        
        # check that temp_varnode refers to the original node, not the new one
        # that had to be recreated when un-pickled.
        self.assertEquals(shifted_ctx[temp_varnode], "temp_varnode_default")
Пример #3
0
    def test_dynamic_varnode_pickle(self):
        # shift B by a temporary varnode with a value set in the root context
        temp_varnode = varnode(default="temp_varnode_default")
        shifted_ctx = self.ctx.shift({B: temp_varnode})
        self.ctx[temp_varnode] = "temp_varnode"
        self.assertEquals(shifted_ctx[B], "temp_varnode")

        # pickle the context, unregister the node and recreate the context
        x = pickle.dumps(self.ctx)
        MDFContext.unregister_node(temp_varnode)
        new_ctx = pickle.loads(x)

        # find the shifted context (can't reshift as temp_varnode was unregistered)
        self.assertEquals(len(new_ctx.get_shifted_contexts()), 1)
        shifted_ctx = new_ctx.get_shifted_contexts()[0]

        # check that B in the shifted context is still set
        self.assertEquals(shifted_ctx[B], "temp_varnode")

        # check that temp_varnode refers to the original node, not the new one
        # that had to be recreated when un-pickled.
        self.assertEquals(shifted_ctx[temp_varnode], "temp_varnode_default")
Пример #4
0
from mdf import (MDFContext, varnode, evalnode, shift, now)

from datetime import datetime, timedelta
from numpy.testing.utils import assert_almost_equal
import pandas as ps
import numpy as np
import unittest
import logging

# this is necessary to stop namespace from looking
# too far up the stack as it looks for the first frame
# not in the mdf package
__package__ = None
_logger = logging.getLogger(__name__)

B = varnode()
C = varnode()

num_calls_A = 0
num_calls_A_override = 0


@evalnode
def A():
    global num_calls_A
    num_calls_A += 1
    return B()


@evalnode
def A_override():
Пример #5
0
from datetime import datetime
from mdf import (
    MDFContext,
    varnode,
    nansumnode,
    evalnode,
    now,
    shift,
)
from numpy.testing.utils import assert_array_almost_equal
from pandas.core import datetools

import pandas as pd
import unittest

A = varnode()

@nansumnode
def B():
    return A() + now().year - 1970

@evalnode
def C():
    while True:
        yield shift(B, A, [1, 2, 3])

@evalnode
def D():
    while True:
        yield shift(B, shift_sets=[{A : 1}, {A : 2}, {A : 3}])
Пример #6
0
)

from datetime import datetime, timedelta
from numpy.testing.utils import assert_almost_equal
import pandas as ps
import numpy as np
import unittest
import logging

# this is necessary to stop namespace from looking
# too far up the stack as it looks for the first frame
# not in the mdf package
__package__ = None
_logger = logging.getLogger(__name__)

B = varnode()
C = varnode()

num_calls_A = 0
num_calls_A_override = 0

@evalnode
def A():
    global num_calls_A
    num_calls_A += 1
    return B()

@evalnode
def A_override():
    global num_calls_A_override
    num_calls_A_override += 1
Пример #7
0
"""
Tests for scenario analysis using multprocessing
"""
import unittest
from mdf.builders.basic import DataFrameBuilder
from mdf import MDFContext, run, varnode, evalnode, now
import pandas as pd
from datetime import datetime
import time

A = varnode()
B = varnode()

@evalnode
def X():
    return A() + B() + now().day


class RemoteTest(unittest.TestCase):

    def setUp(self):
        self.ctx = MDFContext()

    def test_multiprocess_run(self):
        # create a few shifts
        shifts = []
        for i in xrange(10):
            shifts.append({A: i, B: i * 2})

        date_range = pd.bdate_range(datetime(1970, 1, 1), periods=5)
        df_builder = DataFrameBuilder([X])
Пример #8
0
"""
Tests for scenario analysis using multprocessing
"""
import unittest
from mdf.builders.basic import DataFrameBuilder
from mdf import MDFContext, run, varnode, evalnode, now
import pandas as pd
from datetime import datetime
import time

A = varnode()
B = varnode()

@evalnode
def X():
    return A() + B() + now().day


class RemoteTest(unittest.TestCase):

    def setUp(self):
        self.ctx = MDFContext()

    def test_multiprocess_run(self):
        # create a few shifts
        shifts = []
        for i in range(10):
            shifts.append({A: i, B: i * 2})

        date_range = pd.bdate_range(datetime(1970, 1, 1), periods=5)
        df_builder = DataFrameBuilder([X])
Пример #9
0
# not in the mdf package
__package__ = None

_logger = logging.getLogger(__name__)

#
# the following nodes define this graph:
#
#               A       = B + C
#              / \
#  D * E =    B   C     = years since 1970
#            / \   \
#           D   E  now
#

D = varnode()
E = varnode()
F = varnode()


@evalnode
def A():
    return B() + C()


@evalnode
def B():
    return D() * E()


@evalnode
from ..nodes import ConditionalDependencyError
from datetime import datetime, timedelta
from mdf import (
    MDFContext,
    varnode,
    evalnode,
    now, 
    shift
    )

# this is necessary to stop namespace from looking
# too far up the stack as it looks for the first frame
# not in the mdf package
__package__ = None

x = varnode()
y = varnode()
z = varnode()
s = varnode()

@evalnode
def wrap_y():
    return y() ** 2

@evalnode
def deeper_dependency_node():   
    yield x() 
    while True:
        yield wrap_y()

@evalnode
Пример #11
0
import unittest
from ..nodes import ConditionalDependencyError
from datetime import datetime, timedelta
from mdf import (MDFContext, varnode, evalnode, now, shift)

# this is necessary to stop namespace from looking
# too far up the stack as it looks for the first frame
# not in the mdf package
__package__ = None

x = varnode()
y = varnode()
z = varnode()
s = varnode()


@evalnode
def wrap_y():
    return y()**2


@evalnode
def deeper_dependency_node():
    yield x()
    while True:
        yield wrap_y()


@evalnode
def simple_dependency_node():
    yield x()
Пример #12
0
# not in the mdf package
__package__ = None

_logger = logging.getLogger(__name__)

#
# the following nodes define this graph:
#
#               A       = B + C
#              / \
#  D * E =    B   C     = years since 1970
#            / \   \
#           D   E  now
#

D = varnode()
E = varnode()
F = varnode()

@evalnode
def A():
    return B() + C()

@evalnode
def B():
    return D() * E()

@evalnode
def C():
    accum = 0
    cur_year = 1970
Пример #13
0
from datetime import datetime
from mdf import (
    MDFContext,
    varnode,
    nansumnode,
    evalnode,
    now,
    shift,
)
from numpy.testing.utils import assert_array_almost_equal
from pandas.core import datetools

import pandas as pd
import unittest

A = varnode()


@nansumnode
def B():
    return A() + now().year - 1970


@evalnode
def C():
    while True:
        yield shift(B, A, [1, 2, 3])


@evalnode
def D():