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)
    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)
 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)
    def testZeros(self):
        importOp("Zeros")
        v = Vec(32, False)
        op = Zeros()

        op.eval(v)
        self.isAlmostEqualTo(v.getList(), 0.0, 9)
 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)
    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)
 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)
        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)
    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)
    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)
    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)
    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)
 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)
    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)
    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])
    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)
        '''