example005_comparisonPyAbel

This example provides a rather extensive comparison of different openAbel methods with PyAbel methods. It shows how well the main methods of openAbel perform in every regard, especially if the input data is sufficiently smooth.

Since PyAbel’s focus is on the backward (or inverse) transform, this example does it as well.

comparison of different openAbel with PyAbel methods

Comparison of different openAbel with PyAbel methods.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
############################################################################################################################################
# Example to showcase the different openAbel methods in comparison with PyAbel methods.
# Backward transform is done here since PyAbel focuses on that.
# One obviously needs PyAbel installed for this example
############################################################################################################################################


import openAbel as oa
import numpy as np
from scipy.special import erf
import matplotlib.pyplot as mpl
import time as ti
import abel
import os, glob

############################################################################################################################################
# Plotting setup

params = {
   'axes.labelsize': 8,
   'font.size': 8,
   'legend.fontsize': 10,
   'xtick.labelsize': 10,
   'ytick.labelsize': 10,
   'text.usetex': False,
   'figure.figsize': [12., 8.]
   }
mpl.rcParams.update(params)
# Color scheme
colors = ['#005AA9','#E6001A','#99C000','#721085','#EC6500','#009D81','#A60084','#0083CC','#F5A300','#C9D400','#FDCA00']
# Plot markers
markers = ["o", "v" , "s", "D", "p", "*", "h", "+", "^", "x"]
# Line styles 
linestyles = ['-', '--', '-.', ':','-', '--', '-.', ':','-', '--', '-.', ':']
lw = 2

fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = mpl.subplots(2, 3)


############################################################################################################################################
# Error over radius of different methods and orders

def errorAbel(nData, method, order):

    dx = 1./(nData-1);
    xx = np.linspace(0., 1., nData)

    dataIn = 3./8.*np.pi*(1-xx**2)**2
    dataAna = np.sqrt(1-xx**2)**3
    abelObj = oa.Abel(nData, 1, 0., dx, method = method, order = order)
    dataOut = abelObj.execute(dataIn)
    abserr = dataOut-dataAna
    relerr = np.abs(abserr/np.clip(dataAna, 1.e-300, None))

    return (xx, abserr, relerr, dataOut, dataAna)


# Loop over several methods and orders
names = ['HL', 'FMM 2nd', 'FMM 5th']
orders = [-1, 2, 5]
methods = [1, 3, 3]

for ii in range(len(orders)):

    (xx, abserr, relerr, dataOut, dataAna) = errorAbel(30, methods[ii], orders[ii])
    ax1.plot(xx, dataOut, label=str(names[ii]), color = colors[ii], 
             linestyle = linestyles[ii], marker = markers[ii], linewidth=lw)
    ax2.plot(xx, abserr, label=str(names[ii]), color = colors[ii], 
                 linestyle = linestyles[ii], marker = markers[ii], linewidth=lw)
    ax3.semilogy(xx[:-1], relerr[:-1], label=str(names[ii]), color = colors[ii], 
                 linestyle = linestyles[ii], marker = markers[ii], linewidth=lw)

ii += 1
ax1.plot(xx, dataAna, label = 'analytical', color = colors[ii], 
         linestyle = linestyles[ii], marker = markers[ii], linewidth=lw)


def errorAbelPyAbel(nData, method, function):

    dx = 1./(nData-1);
    xx = np.linspace(0., 1., nData)

    dataIn = 3./8.*np.pi*(1-xx**2)**2
    dataAna = np.sqrt(1-xx**2)**3
    dataOut = function(dataIn, basis_dir='.', dr=dx, direction="inverse")
    abserr = dataOut-dataAna
    relerr = np.abs(abserr/np.clip(dataAna, 1.e-300, None))

    return (xx, abserr, relerr, dataOut, dataAna)

jj = ii+1
# Loop over several methods and orders
methods = ['three_point', 'two_point', 'onion_peeling', 
           'hansenlaw', 'basex', 'direct']
functions = [abel.dasch.three_point_transform, abel.dasch.two_point_transform, abel.dasch.onion_peeling_transform,
             abel.hansenlaw.hansenlaw_transform, abel.basex.basex_transform, abel.direct.direct_transform]
for ii in range(len(methods)):

    (xx, abserr, relerr, dataOut, dataAna) = errorAbelPyAbel(30, methods[ii], functions[ii])
    ax1.plot(xx, dataOut, label='PA: '+str(methods[ii]), color = colors[jj+ii], 
             linestyle = linestyles[jj+ii], marker = markers[jj+ii], linewidth=lw)
    ax2.plot(xx, abserr, label='PA: '+str(methods[ii]), color = colors[jj+ii], 
                 linestyle = linestyles[jj+ii], marker = markers[jj+ii], linewidth=lw)
    ax3.semilogy(xx[:-1], relerr[:-1], label='PA: '+str(methods[ii]), color = colors[jj+ii], 
                 linestyle = linestyles[jj+ii], marker = markers[jj+ii], linewidth=lw)


ax1.legend()
ax1.set_xlabel('radius')
ax1.set_ylabel('value')
ax1.grid(True)

ax2.legend()
ax2.set_xlabel('radius')
ax2.set_ylabel('absolute error')
ax2.grid(True)

ax3.legend()
ax3.set_xlabel('radius')
ax3.set_ylabel('relative error')
ax3.grid(True)

#############################################################################################################################################
# Convergence of different methods

def convergenceAbel(nArray, method, order):

    conv = np.empty(nArray.shape[0])
    for ii in range(nArray.shape[0]):

        nData = nArray[ii]
        dx = 1./(nData-1);
        xx = np.linspace(0., 1., nData)
        
        dataIn = 3./8.*np.pi*(1-xx**2)**2
        abelObj = oa.Abel(nData, 1, 0., dx, method = method, order = order)
        dataOut = abelObj.execute(dataIn)
        dataAna = np.sqrt(1-xx**2)**3

        conv[ii] = np.sqrt(np.sum(((dataOut[:-1]-dataAna[:-1])/dataAna[:-1])**2)/(nData-1))

    return conv


# Loop over several methods and orders
names = ['HL', 'FMM 1st', 'FMM 5th']
orders = [-1, 1, 5]
methods = [1, 3, 3]
nArray = 10**(np.arange(5)+2)

for ii in range(len(orders)):
    conv = convergenceAbel(nArray, methods[ii], orders[ii])
    ax4.loglog(nArray, conv, label=str(names[ii]), color = colors[ii], 
               linestyle = linestyles[ii], marker = markers[ii], linewidth=lw)

def convergenceAbelPyAbel(nArray, method, function):

    conv = np.empty(nArray.shape[0])
    for ii in range(nArray.shape[0]):

        nData = nArray[ii]
        dx = 1./(nData-1);
        xx = np.linspace(0., 1., nData)
        
        dataIn = 3./8.*np.pi*(1-xx**2)**2
        dataOut = function(dataIn, basis_dir='.', dr=dx, direction="inverse")
        dataAna = np.sqrt(1-xx**2)**3

        conv[ii] = np.sqrt(np.sum(((dataOut[:-1]-dataAna[:-1])/dataAna[:-1])**2)/(nData-1))
    
    return conv


jj = ii+1
# Loop over several methods
methods = ['hansenlaw', 'onion_peeling', 'three_point', 'two_point', 'basex', 'direct']
functions = [abel.hansenlaw.hansenlaw_transform, abel.dasch.onion_peeling_transform, abel.dasch.three_point_transform,
             abel.dasch.two_point_transform, abel.basex.basex_transform, abel.direct.direct_transform]
for ii in range(1):
    conv = convergenceAbelPyAbel(nArray, methods[ii], functions[ii])
    ax4.loglog(nArray, conv, label='PA: '+str(methods[ii]), color = colors[jj+ii], 
               linestyle = linestyles[jj+ii], marker = markers[jj+ii], linewidth=lw)

nArray = (10**(np.arange(4)*0.5+2)+0.5).astype(int)
for ii in range(1,len(methods)):
    conv = convergenceAbelPyAbel(nArray, methods[ii], functions[ii])
    ax4.loglog(nArray, conv, label='PA: '+str(methods[ii]), color = colors[jj+ii], 
               linestyle = linestyles[jj+ii], marker = markers[jj+ii], linewidth=lw)


ax4.legend()
ax4.set_xlabel('number of data points')
ax4.set_ylabel('relative error')
ax4.grid(True)


#############################################################################################################################################
# Run times of different methods and orders

def runtimesAbel(nArray, nMeasure, method, order):

    runtimes = np.zeros(nArray.shape[0])
    runtimesPre = np.zeros(nArray.shape[0])

    for ii in range(nArray.shape[0]):

        dataIn = np.ones(nArray[ii])
        T = np.empty(nMeasure)
        for jj in range(nMeasure):
            t0 = ti.time()
            abelObj = oa.Abel(nArray[ii], 1, 0., 1., method = method, order = order)
            t1 = ti.time()
            T[jj] = t1-t0
        runtimesPre[ii] = np.sum(T)/nMeasure

        abelObj = oa.Abel(nArray[ii], 1, 0., 1., method = method, order = order)
        t0 = ti.time()
        for jj in range(nMeasure):
            dataOut = abelObj.execute(dataIn)
        t1 = ti.time()

        runtimes[ii] = (t1-t0)/nMeasure

    return (runtimesPre, runtimes)


# Loop over several methods and orders
names = ['HL', 'FMM 3rd', 'FMM 11th']
orders = [-1, 3, 11]
methods = [1, 3, 3]
nArray = 10**(np.arange(5)+2)

for ii in range(3):

    (runtimesPre, runtimes) = runtimesAbel(nArray, 1, methods[ii], orders[ii])
    ax5.loglog(nArray, runtimesPre, label=str(names[ii]), color = colors[ii], 
               linestyle = linestyles[ii], marker = markers[ii], linewidth=lw)
    ax6.loglog(nArray, runtimes, label=str(names[ii]), color = colors[ii], 
               linestyle = linestyles[ii], marker = markers[ii], linewidth=lw)

nArray = 10**(np.arange(4)+2)
for ii in range(3,len(names)):

    (runtimesPre, runtimes) = runtimesAbel(nArray, 1, methods[ii], orders[ii])
    ax5.loglog(nArray, runtimesPre, label=str(names[ii]), color = colors[ii], 
               linestyle = linestyles[ii], marker = markers[ii], linewidth=lw)
    ax6.loglog(nArray, runtimes, label=str(names[ii]), color = colors[ii], 
               linestyle = linestyles[ii], marker = markers[ii], linewidth=lw)


def runtimesAbelPyAbel(nArray, nMeasure, method, function):

    runtimes = np.zeros(nArray.shape[0])
    runtimesPre = np.zeros(nArray.shape[0])

    for ii in range(nArray.shape[0]):

        dataIn = np.ones(nArray[ii])
        T = np.empty(nMeasure)
        for jj in range(nMeasure):
            for filename in glob.glob(method + '*'):
                os.remove(filename) 
            t0 = ti.time()
            dataOut = function(dataIn, basis_dir='.', dr=1., direction="inverse")
            t1 = ti.time()
            T[jj] = t1-t0
        runtimesPre[ii] = np.sum(T)/nMeasure

        t0 = ti.time()
        for jj in range(nMeasure):
            dataOut = function(dataIn, basis_dir='.', dr=1., direction="inverse")
        t1 = ti.time()

        runtimes[ii] = (t1-t0)/nMeasure
        runtimesPre[ii] -= runtimes[ii]

    return (runtimesPre, runtimes)

jj = ii+1
# Loop over several methods
methods = ['hansenlaw', 'onion_peeling', 'three_point', 'two_point', 'basex', 'direct']
functions = [abel.hansenlaw.hansenlaw_transform, abel.dasch.onion_peeling_transform, abel.dasch.three_point_transform,
             abel.dasch.two_point_transform, abel.basex.basex_transform, abel.direct.direct_transform]
nArray = 10**(np.arange(5)+2)
for ii in range(1):
    (runtimesPre, runtimes) = runtimesAbelPyAbel(nArray, 1, methods[ii], functions[ii])
    ax6.loglog(nArray, runtimes, label='PA: '+str(methods[ii]), color = colors[jj+ii], 
               linestyle = linestyles[jj+ii], marker = markers[jj+ii], linewidth=lw)

nArray = (10**(np.arange(4)*0.5+2)+0.5).astype(int)
for ii in range(1,len(methods)):
    (runtimesPre, runtimes) = runtimesAbelPyAbel(nArray, 1, methods[ii], functions[ii])
    ax5.loglog(nArray, runtimesPre, label='PA: '+str(methods[ii]), color = colors[jj+ii], 
               linestyle = linestyles[jj+ii], marker = markers[jj+ii], linewidth=lw)
    ax6.loglog(nArray, runtimes, label='PA: '+str(methods[ii]), color = colors[jj+ii], 
               linestyle = linestyles[jj+ii], marker = markers[jj+ii], linewidth=lw)

ax5.legend()
ax5.set_xlabel('number of data points')
ax5.set_ylabel('run time pre computation in s')
ax5.grid(True)

ax6.legend()
ax6.set_xlabel('number of data points')
ax6.set_ylabel('run time main computation in s')
ax6.grid(True)


mpl.tight_layout()
mpl.savefig('example005_comparisonPyAbel.png', dpi=300)

mpl.show()