def getPars(self):
     #filename = os.path.join("test","lena_gray_512.tif")
     #plt = TiffReader()
     filename = os.path.join("test", "test_imageLena.bin")
     plt = BinReader()
     # read image
     Im = plt.imread(filename)
     Im = np.asarray(Im, dtype='float32')
     Im = Im / 255
     perc = 0.05
     u0 = Im + np.random.normal(loc=0, scale=perc * Im, size=np.shape(Im))
     u_ref = Im + np.random.normal(
         loc=0, scale=0.01 * Im, size=np.shape(Im))
     u0 = u0.astype('float32')
     u_ref = u_ref.astype('float32')
     return Im, u0, u_ref
    def test_FGP_TV_CPU_vs_GPU(self):
        print(__name__)
        #filename = os.path.join("test","lena_gray_512.tif")
        #plt = TiffReader()
        filename = os.path.join("test", "test_imageLena.bin")
        plt = BinReader()
        # read image
        Im = plt.imread(filename)
        Im = np.asarray(Im, dtype='float32')

        Im = Im / 255
        perc = 0.05
        u0 = Im + np.random.normal(loc=0, scale=perc * Im, size=np.shape(Im))
        u_ref = Im + np.random.normal(
            loc=0, scale=0.01 * Im, size=np.shape(Im))

        # map the u0 u0->u0>0
        # f = np.frompyfunc(lambda x: 0 if x < 0 else x, 1,1)
        u0 = u0.astype('float32')
        u_ref = u_ref.astype('float32')

        print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
        print("____________FGP-TV bench___________________")
        print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")

        # set parameters
        pars = {'algorithm' : FGP_TV, \
        'input' : u0,\
        'regularisation_parameter':0.02, \
        'number_of_iterations' :400 ,\
        'tolerance_constant':0.0,\
        'methodTV': 0 ,\
        'nonneg': 0}

        print("#############FGP TV CPU####################")
        start_time = timeit.default_timer()
        (fgp_cpu, infocpu) = FGP_TV(pars['input'],
                                    pars['regularisation_parameter'],
                                    pars['number_of_iterations'],
                                    pars['tolerance_constant'],
                                    pars['methodTV'], pars['nonneg'], 'cpu')

        rms = rmse(Im, fgp_cpu)
        pars['rmse'] = rms

        txtstr = printParametersToString(pars)
        txtstr += "%s = %.3fs" % ('elapsed time',
                                  timeit.default_timer() - start_time)
        print(txtstr)

        print("##############FGP TV GPU##################")
        start_time = timeit.default_timer()
        try:
            (fgp_gpu,
             infogpu) = FGP_TV(pars['input'], pars['regularisation_parameter'],
                               pars['number_of_iterations'],
                               pars['tolerance_constant'], pars['methodTV'],
                               pars['nonneg'], 'gpu')

        except ValueError as ve:
            self.skipTest("Results not comparable. GPU computing error.")

        rms = rmse(Im, fgp_gpu)
        pars['rmse'] = rms
        pars['algorithm'] = FGP_TV
        txtstr = printParametersToString(pars)
        txtstr += "%s = %.3fs" % ('elapsed time',
                                  timeit.default_timer() - start_time)
        print(txtstr)

        print("--------Compare the results--------")
        tolerance = 1e-05
        diff_im = np.zeros(np.shape(fgp_cpu))
        diff_im = abs(fgp_cpu - fgp_gpu)
        diff_im[diff_im > tolerance] = 1

        self.assertLessEqual(diff_im.sum(), 1)
    def test_Diff4th_CPU_vs_GPU(self):
        #filename = os.path.join("test","lena_gray_512.tif")
        #plt = TiffReader()
        # read image
        filename = os.path.join("test", "test_imageLena.bin")
        plt = BinReader()
        Im = plt.imread(filename)
        Im = np.asarray(Im, dtype='float32')

        Im = Im / 255
        perc = 0.05
        u0 = Im + np.random.normal(loc=0, scale=perc * Im, size=np.shape(Im))
        u_ref = Im + np.random.normal(
            loc=0, scale=0.01 * Im, size=np.shape(Im))

        # map the u0 u0->u0>0
        # f = np.frompyfunc(lambda x: 0 if x < 0 else x, 1,1)
        u0 = u0.astype('float32')
        u_ref = u_ref.astype('float32')

        print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
        print("___Anisotropic Diffusion 4th Order (2D)____")
        print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")

        # set parameters
        pars = {'algorithm' : Diff4th, \
        'input' : u0,\
        'regularisation_parameter':0.8, \
        'edge_parameter':0.02,\
        'number_of_iterations' :1000 ,\
        'time_marching_parameter':0.0001,\
        'tolerance_constant':0.0}

        print("#############Diff4th CPU####################")
        start_time = timeit.default_timer()
        (diff4th_cpu, info_vec_cpu) = Diff4th(
            pars['input'], pars['regularisation_parameter'],
            pars['edge_parameter'], pars['number_of_iterations'],
            pars['time_marching_parameter'], pars['tolerance_constant'], 'cpu')

        rms = rmse(Im, diff4th_cpu)
        pars['rmse'] = rms

        txtstr = printParametersToString(pars)
        txtstr += "%s = %.3fs" % ('elapsed time',
                                  timeit.default_timer() - start_time)
        print(txtstr)
        print("##############Diff4th GPU##################")
        start_time = timeit.default_timer()
        try:
            (diff4th_gpu, info_vec_gpu) = Diff4th(
                pars['input'], pars['regularisation_parameter'],
                pars['edge_parameter'], pars['number_of_iterations'],
                pars['time_marching_parameter'], pars['tolerance_constant'],
                'gpu')

        except ValueError as ve:
            self.skipTest("Results not comparable. GPU computing error.")
        rms = rmse(Im, diff4th_gpu)
        pars['rmse'] = rms
        pars['algorithm'] = Diff4th
        txtstr = printParametersToString(pars)
        txtstr += "%s = %.3fs" % ('elapsed time',
                                  timeit.default_timer() - start_time)
        print(txtstr)
        print("--------Compare the results--------")
        tolerance = 1e-05
        diff_im = np.zeros(np.shape(diff4th_cpu))
        diff_im = abs(diff4th_cpu - diff4th_gpu)
        diff_im[diff_im > tolerance] = 1
        self.assertLessEqual(diff_im.sum(), 1)