Ejemplo n.º 1
0
def test_average_subtraction(qtbot):
    """Test the subtract average filter node"""

    SubtractAverage.useUi = False
    SubtractAverage.uiClass = None

    fc = linearFlowchart(('Subtract Average', SubtractAverage), )
    node = fc.nodes()['Subtract Average']

    x = np.arange(11) - 5.
    y = np.linspace(0, 10, 51)
    xx, yy = np.meshgrid(x, y, indexing='ij')
    zz = np.sin(yy) + xx
    zz_ref_avg_y = np.sin(yy) - np.sin(yy).mean()

    data = MeshgridDataDict(x=dict(values=xx),
                            y=dict(values=yy),
                            z=dict(values=zz, axes=['x', 'y']))
    assert data.validate()

    fc.setInput(dataIn=data)
    assert num.arrays_equal(zz, fc.outputValues()['dataOut'].data_vals('z'))

    node.averagingAxis = 'y'
    assert num.arrays_equal(
        zz_ref_avg_y,
        fc.outputValues()['dataOut'].data_vals('z'),
    )
Ejemplo n.º 2
0
def subtractAverage():
    x = np.arange(11) - 5.
    y = np.linspace(0, 10, 51)
    xx, yy = np.meshgrid(x, y, indexing='ij')
    zz = np.sin(yy) + xx
    data = MeshgridDataDict(x=dict(values=xx),
                            y=dict(values=yy),
                            z=dict(values=zz, axes=['x', 'y']))
    data.validate()

    x = np.arange(11) - 5.
    y = np.linspace(0, 10, 51)
    xx, yy = np.meshgrid(x, y, indexing='ij')
    zz = np.sin(yy) + xx
    data2 = MeshgridDataDict(reps=dict(values=xx),
                             y=dict(values=yy),
                             z=dict(values=zz, axes=['reps', 'y']))
    data2.validate()

    # make app and gui, fc
    app = QtGui.QApplication([])
    win, fc = makeFlowchartWithAutoPlotWindow([('sub', SubtractAverage)])
    win.show()

    # feed in data
    fc.setInput(dataIn=data)
    fc.setInput(dataIn=data2)

    return app.exec_()
Ejemplo n.º 3
0
def test_xy_selector_with_roles(qtbot):
    """Testing XY selector using the roles 'meta' property."""

    XYSelector.uiClass = None

    fc = linearFlowchart(('xysel', XYSelector))
    node = fc.nodes()['xysel']

    x = np.arange(5.0)
    y = np.linspace(0, 1, 5)
    z = np.arange(4.0, 6.0, 1.0)
    xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')
    vals = xx * yy * zz
    data = MeshgridDataDict(x=dict(values=xx),
                            y=dict(values=yy),
                            z=dict(values=zz),
                            vals=dict(values=vals, axes=['x', 'y', 'z']))
    assert data.validate()

    fc.setInput(dataIn=data)

    # this should return None, because no x/y axes were set.
    assert fc.outputValues()['dataOut'] is None

    # now select two axes, and test that the other one is correctly selected
    node.xyAxes = ('x', 'y')

    assert num.arrays_equal(fc.outputValues()['dataOut'].data_vals('vals'),
                            vals[:, :, 0])
    assert node.dimensionRoles == {
        'x': 'x-axis',
        'y': 'y-axis',
        'z': (ReductionMethod.elementSelection, [], {
            'index': 0,
            'axis': 2
        })
    }

    # now set the role directly through the meta property
    node.dimensionRoles = {
        'x': 'y-axis',
        'y': (ReductionMethod.average, [], {}),
        'z': 'x-axis',
    }

    assert node.xyAxes == ('z', 'x')
    assert num.arrays_equal(fc.outputValues()['dataOut'].data_vals('vals'),
                            vals[:, :, :].mean(axis=1).transpose((1, 0)))
Ejemplo n.º 4
0
def test_xy_selector(qtbot):
    """Basic XY selector node test."""

    XYSelector.uiClass = None

    fc = linearFlowchart(('xysel', XYSelector))
    node = fc.nodes()['xysel']

    x = np.arange(5.0)
    y = np.linspace(0, 1, 5)
    z = np.arange(4.0, 6.0, 1.0)
    xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')
    vals = xx * yy * zz
    data = MeshgridDataDict(x=dict(values=xx),
                            y=dict(values=yy),
                            z=dict(values=zz),
                            vals=dict(values=vals, axes=['x', 'y', 'z']))
    assert data.validate()

    fc.setInput(dataIn=data)

    # this should return None, because no x/y axes were set.
    assert fc.outputValues()['dataOut'] is None

    # now select two axes, and test that the other one is correctly selected
    node.xyAxes = ('x', 'y')
    assert num.arrays_equal(fc.outputValues()['dataOut'].data_vals('vals'),
                            vals[:, :, 0])

    # try a different reduction on the third axis
    node.reductions = {'z': (ReductionMethod.average, [], {})}
    assert num.arrays_equal(fc.outputValues()['dataOut'].data_vals('vals'),
                            vals.mean(axis=-1))

    # Test transposing the data by flipping x/y
    node.xyAxes = ('y', 'x')
    assert num.arrays_equal(fc.outputValues()['dataOut'].data_vals('vals'),
                            vals.mean(axis=-1).transpose((1, 0)))
Ejemplo n.º 5
0
def test_reduction(qtbot):
    """Test basic dimension reduction."""
    DimensionReducer.uiClass = None

    fc = linearFlowchart(('dim_red', DimensionReducer))
    node = fc.nodes()['dim_red']

    x = np.arange(5.0)
    y = np.linspace(0, 1, 5)
    z = np.arange(4.0, 6.0, 1.0)
    xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')
    vals = xx * yy * zz
    data = MeshgridDataDict(x=dict(values=xx),
                            y=dict(values=yy),
                            z=dict(values=zz),
                            vals=dict(values=vals, axes=['x', 'y', 'z']))
    assert data.validate()

    fc.setInput(dataIn=data)
    assert num.arrays_equal(fc.outputValues()['dataOut'].data_vals('vals'),
                            vals)

    node.reductions = {'y': (np.mean, [], {})}

    out = fc.outputValues()['dataOut']
    assert num.arrays_equal(vals.mean(axis=1), out.data_vals('vals'))
    assert out.axes('vals') == ['x', 'z']

    node.reductions = {
        'y': (ReductionMethod.elementSelection, [], {
            'index': 0
        }),
        'z': (ReductionMethod.average, )
    }

    out = fc.outputValues()['dataOut']
    assert num.arrays_equal(vals[:, 0, :].mean(axis=-1), out.data_vals('vals'))
    assert out.axes('vals') == ['x']
Ejemplo n.º 6
0
def makeData():
    xvals = np.linspace(0, 10, 51)
    reps = np.arange(20)
    xx, rr = np.meshgrid(xvals, reps, indexing='ij')
    data = sinefunc(xx, amp=0.8, freq=0.25, phase=0.1)
    noise = np.random.normal(scale=0.2, size=data.shape)
    data += noise

    dd = MeshgridDataDict(
        x=dict(values=xx),
        repetition=dict(values=rr),
        sine=dict(values=data, axes=['x', 'repetition']),
    )
    return dd
Ejemplo n.º 7
0
def makeData():
    xvals = np.linspace(0, 10, 51)
    reps = np.arange(20)
    xx, rr = np.meshgrid(xvals, reps, indexing='ij')
    data = sinefunc(xx, amp=0.8, freq=0.25, phase=0.1)
    noise = np.random.normal(scale=0.2, size=data.shape)
    data += noise

    params = lmfit.Parameters()
    for pn, pv in Cosine.guess(xvals, data[0]).items():
        params.add(pn, value=pv)
    fitting_options = FittingOptions(Cosine, params)

    dd = MeshgridDataDict(x=dict(values=xx),
                          repetition=dict(values=rr),
                          sine=dict(values=data, axes=['x', 'repetition']),
                          __fitting_options__=fitting_options)
    return dd
Ejemplo n.º 8
0
def test_basics():
    """Creation and validation of the meshgrid data dict."""
    x = np.arange(3)
    y = np.arange(1, 4)
    xx, yy = np.meshgrid(x, y, indexing='ij')
    zz = xx * yy

    dd = MeshgridDataDict(x=dict(values=xx),
                          y=dict(values=yy),
                          z=dict(values=zz, axes=['x', 'y']))

    assert dd.validate()
    assert dd.shape() == xx.shape

    dd = MeshgridDataDict(x=dict(values=x),
                          y=dict(values=y),
                          z=dict(values=zz, axes=['x', 'y']))

    with pytest.raises(ValueError):
        dd.validate()
Ejemplo n.º 9
0
def test_reorder():
    """Test reordering of axes."""

    a = np.arange(3)
    b = np.arange(5, 10)
    c = np.linspace(0, 1, 3)
    aa, bb, cc = np.meshgrid(a, b, c, indexing='ij')
    zz = aa + bb + cc

    dd = MeshgridDataDict(a=dict(values=aa),
                          b=dict(values=bb),
                          c=dict(values=cc),
                          z=dict(values=zz, axes=['a', 'b', 'c']))

    assert dd.validate()
    dd = dd.reorder_axes(c=0)
    assert dd.axes('z') == ['c', 'a', 'b']
    assert num.arrays_equal(dd.data_vals('a'), aa.transpose([2, 0, 1]))
    assert num.arrays_equal(dd.data_vals('b'), bb.transpose([2, 0, 1]))
    assert num.arrays_equal(dd.data_vals('c'), cc.transpose([2, 0, 1]))
    assert num.arrays_equal(dd.data_vals('z'), zz.transpose([2, 0, 1]))