Ejemplo n.º 1
0
    def testZeros(self):
        importOp("Zeros")
        v = Vec(32, False)
        op = Zeros()

        op.eval(v)
        self.isAlmostEqualTo(v.getList(), 0.0, 9)
Ejemplo n.º 2
0
 def testTriInverse(self):
     from org.nmrfx.processor.operations import Tri
     op = Tri(0, 1.0, 1.0, False)
     v = Vec(1000, False)
     v.ones()
     op.eval(v)
     iop = op.getInverseOp()
     iop.eval(v)
     self.isAlmostEqualTo(v.getList(), 1.0)
Ejemplo n.º 3
0
    def testAdd(self):
        importOp('Add')
        v = Vec(256, True)
        temp = v.getList()
        real, imag = 1, 3
        op = Add(real, imag, 0, -1)

        op.eval(v)
        for i, v in enumerate(v.getList()):
            self.assertAlmostEqual(v.real, temp[i].real + real)
            self.assertAlmostEqual(v.imag, temp[i].imag + imag)
Ejemplo n.º 4
0
 def testSinebellApodInverseComplex(self):
     v = Vec(1024, True)
     v.ones()
     offset = 0.5
     end = 0.98
     power = 1.0
     c = 1.0
     apodSize = 0
     op = SinebellApod(offset, end, power, c, apodSize, False)
     op.eval(v)
     iop = op.getInverseOp()
     iop.eval(v)
     self.isAlmostEqualTo(v.getList(), 1.0)
Ejemplo n.º 5
0
    def testOperation(self):
        importOp("Operation")

        class TestOperation(Operation):
            __slots__ = ['a', 'b', 'c']

            def __init__(self, a, b, c):
                self.a = a
                self.b = b
                self.c = c

            def eval(self, vector):
                if len(vector) > 3:
                    vector.set(0, self.a, 0)
                    vector.set(1, self.b, 0)
                    vector.set(2, self.c, 0)
                    vector.set(3, self.a * self.b, self.c)

        a, b, c = 2, 3, 4
        op = TestOperation(a, b, c)
        v = Vec(32, True)
        op.eval(v)

        self.assertEqual(v[0].real, a)
        self.assertEqual(v[1].real, b)
        self.assertEqual(v[2].real, c)
        self.assertEqual(v[3].real, a * b)
        self.assertEqual(v[3].imag, c)
Ejemplo n.º 6
0
 def genSpectrum(self, size=1024):
     importOp("Gen")
     importOp("Ft")
     v = Vec(size, True)
     sw = 1000.0
     dwell = 1.0 / sw
     v.setDwellTime(dwell)
     freq = sw / 4.0
     lw = 10.0
     amp = 100
     ph = 0.0
     op = Gen(freq, lw, amp, ph)
     op.eval(v)
     op = Ft(False, False)
     op.eval(v)
     return v
Ejemplo n.º 7
0
    def testZFInverse(self):
        from org.nmrfx.processor.operations import Zf

        size = 31
        newSize = 64
        factor = None
        pad = None
        v = Vec(size, False)

        op = Zf(factor, newSize, pad)
        op.eval(v)
        size2 = v.getSize()
        self.assert_(newSize == size2)

        iop = op.getInverseOp()
        iop.eval(v)
        size3 = v.getSize()
        self.assert_(size == size3)
Ejemplo n.º 8
0
        def test_case(size, pt1, pt2):
            '''Pass in pt1, pt2, and vector size and verify that it is correct'''
            v = Vec(size, False)
            v.ones()
            op = Tm(pt1, pt2)
            op.eval(v)

            for i in range(0, pt1):
                self.assert_(v[i] < v[i + 1])

            for i in range(pt1, pt2):
                self.assert_(v[i] == v[i + 1])

            for i in range(pt2, size - 1):
                self.assert_(v[i] > v[i + 1])
            self.assertAlmostEqual(v[pt1], 1.0)
            self.assertAlmostEqual(v[pt1 / 2], 0.5, 5)
            self.assertAlmostEqual(v[pt2], 1.0)
            self.assertAlmostEqual(v[pt2 + (size - pt2) / 2], 0.5, 5)
Ejemplo n.º 9
0
 def testIft(self):
     importOp("Ft")
     importOp("Ift")
     importOp("Gen")
     size = 1024
     v = Vec(size, True)
     vSave = Vec(size, True)
     sw = 1000.0
     dwell = 1.0 / sw
     v.setDwellTime(dwell)
     op = Gen(1000.0, 10.0, 100.0, 0.0)
     op.eval(v)
     v.copy(vSave)
     op = Ft(False, False)
     op.eval(v)
     op = Ift()
     op.eval(v)
     v.subtract(vSave)
     self.isAlmostEqualTo(v.getList(), 0.0)
Ejemplo n.º 10
0
    def testApodization(self):
        importOp('Apodization')

        class MultByLineApod(Apodization):
            def __init__(self, size):
                self.resize(size)
                self.setApodVec([float(i) / 256 for i in range(256)])

            def eval(self, vector):
                self.applyApod(vector)
                return self

        v = Vec(256, True)
        v.ones()

        op = MultByLineApod(256)

        op.eval(v)
        temp = v.getList()
        for i, v in enumerate(temp):
            self.assertEqual(i / 256.0, v)
Ejemplo n.º 11
0
    def testFtInverse(self):
        importOp("Ft")
        importOp("Gen")
        size = 1024
        v = self.genSpectrum(size)
        vSave = Vec(size, True)
        v.copy(vSave)

        op = Ft(False, False)
        op.eval(v)
        iop = op.getInverseOp()
        iop.eval(v)
        v.subtract(vSave)
        self.isAlmostEqualTo(v.getList(), 0.0)

        vSave.copy(v)
        op = Ft(True, False)
        op.eval(v)
        iop = op.getInverseOp()
        iop.eval(v)
        v.subtract(vSave)
        self.isAlmostEqualTo(v.getList(), 0.0)

        vSave.copy(v)
        op = Ft(False, True)
        op.eval(v)
        iop = op.getInverseOp()
        iop.eval(v)
        v.subtract(vSave)
        self.isAlmostEqualTo(v.getList(), 0.0)
Ejemplo n.º 12
0
    def testHft(self):
        importOp("Hft")
        size = 1024
        v = self.genSpectrum(size)
        vSave = Vec(size, True)
        v.copy(vSave)
        v.makeReal()

        op = Hft()
        op.eval(v)
        v.subtract(vSave)
        # fixme  Hft generates some wiggle in baseline which requires relatively large tolerance here
        self.isAlmostEqualTo(v.getList(), 0.0, 3)
Ejemplo n.º 13
0
        def test_case(size=10, shift=1, complex=False, useApache=False):
            v = Vec(10, complex)
            if (useApache):
                v.makeApache()

            v.ones()
            v.rand()

            temp = v.getList()

            shift = 3
            op = Shift(shift)
            op.eval(v)

            for index, val in enumerate(v.getList()):
                if index < shift:
                    self.assertEqual(val.real, 0)
                    if complex:
                        self.assertEqual(val.imag, 0)
                else:
                    self.assertEqual(val.real, temp[index - shift].real)
                    if complex:
                        self.assertEqual(val.imag, temp[index - shift].imag)
Ejemplo n.º 14
0
 def testOnes(self):
     importOp("Ones")
     v = Vec(32, False)
     op = Ones()
     op.eval(v)
     self.isAlmostEqualTo(v.getList(), 1.0, 9)
     v = Vec(32, True)
     op.eval(v)
     self.isAlmostEqualTo(v.getList(), 1.0, 9)
Ejemplo n.º 15
0
 def testImag(self):
     importOp("Imag")
     size = 8
     v = Vec(size, True)
     v.set(0, 1.0, 2.0)
     v.set(1, 3.0, 4.0)
     values = v.getList()
     op = Imag()
     op.eval(v)
     values = v.getList()
     self.assert_(not v.isComplex())
     self.assert_(values[0].real == 2.0)
     self.assert_(values[1].real == 4.0)
Ejemplo n.º 16
0
    def testZF(self):
        from org.nmrfx.processor.operations import Zf
        import math
        log2 = lambda x: int(math.log(x) / math.log(2))

        size = 32
        newSize = 63
        factor = None
        pad = None
        v = Vec(size, False)

        op = Zf(factor, newSize, pad)
        op.eval(v)

        self.assert_(len(v) == newSize)
        self.assert_(v.getSize() == newSize)

        size = 64
        v = Vec(size, False)
        newSize = None
        factor = 2
        pad = None

        op = Zf(factor, newSize, pad)
        op.eval(v)
        self.assert_(len(v) == 2**(2 + log2(size)))
        self.assert_(v.getSize() == 2**(2 + log2(size)))

        size = 100
        v = Vec(size, False)
        newSize = None
        factor = None
        pad = 15

        op = Zf(factor, newSize, pad)
        op.eval(v)
        self.assert_(len(v) == size + pad)
        self.assert_(v.getSize() == size + pad)
Ejemplo n.º 17
0
    def testExpdInverse(self):
        importOp("Expd")
        from math import pi, exp
        v = Vec(1025, False)
        v.ones()
        dwell = 1.0 / 1000.0
        v.setDwellTime(dwell)

        lb = 2.0
        fPoint = 1.0

        op = Expd(lb, fPoint, False)
        op.eval(v)
        iop = op.getInverseOp()
        iop.eval(v)
        self.isAlmostEqualTo(v.getList(), 1.0)
Ejemplo n.º 18
0
    def testSinebellApod(self):
        importOp("SinebellApod")
        from math import pi, sin
        #from 0 to Pi inclusive
        v = Vec(1025, False)
        v.ones()

        #offset = 0.5
        offset = 0.0
        end = 1.0
        power = 1.0
        c = 1.0
        apodSize = 0

        temp = v.getList()
        op = SinebellApod(offset, end, power, c, apodSize, False)

        op.eval(v)

        for val, index in zip([sin(i) for i in [pi * j / 4 for j in range(5)]],
                              [0, 256, 512, 768, 1024]):
            self.assertAlmostEqual(val, v[index])

        offset = 0.5
        end = 1.5

        v = Vec(1025, False)
        v.ones()

        op = SinebellApod(offset, end, power, c, apodSize, False)

        op.eval(v)
        for val, index in zip(
            [sin(i) for i in [.5 * pi + pi * j / 4 for j in range(5)]],
            [0, 256, 512, 768, 1024]):
            self.assertAlmostEqual(val, v[index])
Ejemplo n.º 19
0
    def testExpd(self):
        importOp("Expd")
        from math import pi, exp
        v = Vec(1025, False)
        v.ones()
        dwell = 1.0 / 1000.0
        v.setDwellTime(dwell)

        lb = 2.0
        fPoint = 1.0
        op = Expd(lb, fPoint, False)
        op.eval(v)
        decay = pi * lb

        for index, val in enumerate(v.getList()):
            testVal = exp(-1.0 * index * decay * dwell)
            self.assertAlmostEqual(val, testVal)
Ejemplo n.º 20
0
    def testGf(self):
        importOp("Gf")
        from math import pi, exp
        v = Vec(1025, False)
        v.ones()
        dwell = 1.0 / 1000.0
        v.setDwellTime(dwell)

        gf = 0.5
        gfs = 0.1
        fPoint = 1.0

        op = Gf(gf, gfs, fPoint)
        op.eval(v)
Ejemplo n.º 21
0
    def testGmbInverse(self):
        importOp("Gmb")
        from math import pi, exp
        v = Vec(1025, False)
        v.ones()
        dwell = 1.0 / 1000.0
        v.setDwellTime(dwell)

        gb = 0.1
        lb = 2.0
        fPoint = 1.0

        op = Gmb(gb, lb, fPoint)
        op.eval(v)
        iop = op.getInverseOp()
        iop.eval(v)

        self.isAlmostEqualTo(v.getList(), 1.0)
Ejemplo n.º 22
0
    def testGfInverse(self):
        importOp("Gf")
        from math import pi, exp
        v = Vec(1025, False)
        v.ones()
        dwell = 1.0 / 1000.0
        v.setDwellTime(dwell)

        gf = 0.5
        gfs = 0.1
        fPoint = 1.0

        op = Gf(gf, gfs, fPoint)
        op.eval(v)
        iop = op.getInverseOp()
        iop.eval(v)

        self.isAlmostEqualTo(v.getList(), 1.0)
Ejemplo n.º 23
0
 def testDx(self):
     v = Vec(1000, False)
     v = v.rand()
Ejemplo n.º 24
0
    def testGen(self):
        importOp("Gen")
        size = 1024
        v = Vec(size, True)
        sw = 1000.0
        dwell = 1.0 / sw
        v.setDwellTime(dwell)
        freq = sw / 4.0
        lw = 10.0
        amp = 100
        ph = 0.0
        op = Gen(freq, lw, amp, ph)
        op.eval(v)
        values = v.getList()
        # check amplitude is amp
        self.assertAlmostEqual(values[0], amp)

        v.zeros()
        op = Gen(freq, lw, amp, 90.0)
        op.eval(v)
        values = v.getList()
        # check imaginary  amplitude is amp when phase is 90
        self.assertAlmostEqual(values[0].imag, amp)

        v.zeros()
        op = Gen(freq, lw, amp, 0.0)
        op.eval(v)
        v.ft()
        peakCenter = size * 3 / 4
        values = v.getList()
        # check if expected peak position is max
        self.assert_(v[peakCenter - 1].real < v[peakCenter].real)
        self.assert_(v[peakCenter + 1].real < v[peakCenter].real)
        # check if peak is symmetrical
        self.assertAlmostEqual(values[peakCenter - 2].real,
                               values[peakCenter + 2].real)

        v.zeros()
        op = Gen(freq, lw, amp, 0.0)
        op.eval(v)
        op.eval(v)
        values = v.getList()
        # check if additive (successive calls don't zero first)
        self.assertAlmostEqual(values[0].real, 2.0 * amp)
Ejemplo n.º 25
0
    def offtestIstCL(self):
        importOp("IstCL")
        importOp("Ft")
        importOp("Add")
        import pyproc
        from java.util import ArrayList

        #testing single Vector FFT with JavaCL and comparing the result to
        #the vector.ft method
        vec = Vec(1024, True)
        self.assert_(vec.useApache())
        pyproc.GEN(vector=vec)
        vectors = ArrayList()
        vectors.add(vec)

        op = IstCL(1)
        op.initializeVectors(vectors)
        op.initializeBuffers()
        #op.onePass(vectors)
        op.parallelfft(False)
        op.copyBack(vectors, False)
        op.finish()

        vec2 = Vec(1024, True)
        pyproc.GEN(vector=vec2)

        op = Ft(False, False)
        op.eval(vec2)

        #vec2.ft()

        #testing fft
        for (v1, v2) in zip(vec.getList(), vec2.getList()):
            self.assertAlmostEqual(v1.real, v2.real, 12)
            self.assertAlmostEqual(v1.imag, v2.imag, 12)
        '''
        test norm
        '''
        vec1 = Vec(32, True)
        vec2 = Vec(32, True)

        add = Add(100.0, 100.0)
        add.eval(vec1)
        add.eval(vec2)

        set_max_index = 31

        vec1.set(set_max_index, 101.0, 101.0)
        vec2.set(set_max_index, 101.0, 101.0)

        op = IstCL(1)
        vectors = ArrayList()
        vectors.add(vec1)

        op.initializeVectors(vectors)
        op.initializeBuffers()
        op.declareComplexNorm()
        op.declareReduceMax()
        op.norm()
        norms = op.copyBackNormBuffer()
        op.reduceMax()
        m = op.copyBackMaxBuffer()
        index = op.copyBackMaxIndexBuffer()
        print index
        op.finish()

        vec2norm = [i.real**2 + i.imag**2 for i in vec2.getList()]
        for v1, v2 in zip(norms, vec2norm):
            self.assertEqual(v1, v2)

        self.assertEqual(m, 101 * 101 + 101 * 101)
        self.assertAlmostEqual(index, set_max_index)

        #for (v1, v2) in zip(vec1.getList(), vec2.getList()):
        #    self.assertAlmostEqual(v1.real, v2.real, 12)
        #    self.assertAlmostEqual(v1.imag, v2.imag, 12)
        '''Test thresholding without fft'''
        vec1 = Vec(256, True)
        vec2 = Vec(256, True)

        vec1.ones()
        vec2.ones()

        vec1.multiply(100.0, 0)
        vec2.multiply(3.04, 0)
        #vec2.ft();
        #vec2.ift();
        #if we have a threshold of .96, then all values will be thresholded by
        #96.96 and will be reduced to 3.04
        vec1.setComplex(2, 101.0, 0.0)
        vec2.setComplex(2, 3.04, 0.0)

        op = IstCL(1)
        vectors = ArrayList()
        vectors.add(vec1)
        op.initializeVectors(vectors)
        op.initializeBuffers()
        op.norm()
        op.reduceMax()
        op.threshold()
        op.copyBack(vectors, True)

        op.finish()

        for (i, (v1, v2)) in enumerate(zip(vec1.getList(), vec2.getList())):
            print i
            self.assertAlmostEqual(v1.real, v2.real, 12)
            self.assertAlmostEqual(v1.imag, v2.imag, 12)
        '''
Ejemplo n.º 26
0
    def testTri(self):
        from org.nmrfx.processor.operations import Tri
        op = Tri(0, 1.0, 1.0, False)

        v = Vec(1000, False)
        v.ones()
        op.eval(v)
        for i in range(v.getSize()):
            self.assertAlmostEqual(v[i], 1.0)
        self.assertEqual(v.getReal(0), 1.0)
        self.assertEqual(v.getReal(999), 1.0)

        op = Tri(500, -10.0, 10.0, False)
        v1 = Vec(1000, False)
        v1.ones()
        op.eval(v1)
        self.assertAlmostEqual(v1.getReal(0), -10.0)
        self.assertAlmostEqual(v1.getReal(999), 10.0)

        for i in range(v.getSize() - 1):
            self.assert_(v[i] <= v[i + 1])

        v2 = Vec(100, False)
        v2.ones()
        op = Tri(0, 0.0, 2.0, False)
        op.eval(v2)
        self.assertAlmostEqual(v2.getReal(0), 1.0)
        self.assertAlmostEqual(v2.getReal(49), 1.5, 1)
        self.assertAlmostEqual(v2.getReal(99), 2.0)

        for i in range(v.getSize() - 1):
            self.assert_(v[i] <= v[i + 1])