Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

# coding: utf-8 

# Copyright (c) Pymatgen Development Team. 

# Distributed under the terms of the MIT License. 

 

from __future__ import unicode_literals, division, print_function 

 

""" 

function for calculating the convergence of an x, y data set 

main api: 

 

test_conv(xs, ys, name, tol) 

 

tries to fit multiple functions to the x, y data 

 

calculates which function fits best 

for tol < 0 

returns the x value for which y is converged within tol of the assymtotic value 

for tol > 0 

returns the x_value for which dy(x)/dx < tol for all x >= x_value, conv is true is such a x_value exists 

for the best fit a gnuplot line is printed plotting the data, the function and the assymthotic value 

""" 

 

__author__ = "Michiel van Setten" 

__copyright__ = " " 

__version__ = "0.9" 

__maintainer__ = "Michiel van Setten" 

__email__ = "mjvansetten@gmail.com" 

__date__ = "June 2014" 

 

import string 

import random 

import numpy as np 

 

 

def id_generator(size=8, chars=string.ascii_uppercase + string.digits): 

return ''.join(random.choice(chars) for _ in range(size)) 

 

 

class SplineInputError(Exception): 

def __init__(self, msg): 

self.msg = msg 

 

 

def get_derivatives(xs, ys, fd=False): 

""" 

return the derivatives of y(x) at the points x 

if scipy is available a spline is generated to calculate the derivatives 

if scipy is not available the left and right slopes are calculated, if both exist the average is returned 

putting fd to zero always returns the finite difference slopes 

""" 

try: 

if fd: 

raise SplineInputError('no spline wanted') 

if len(xs) < 4: 

er = SplineInputError('too few data points') 

raise er 

from scipy.interpolate import UnivariateSpline 

spline = UnivariateSpline(xs, ys) 

d = spline.derivative(1)(xs) 

except (ImportError, SplineInputError): 

d = [] 

m, left, right = 0, 0, 0 

for n in range(0, len(xs), 1): 

try: 

left = (ys[n] - ys[n-1]) / (xs[n] - xs[n-1]) 

m += 1 

except IndexError: 

pass 

try: 

right = (ys[n+1] - ys[n]) / (xs[n+1] - xs[n]) 

m += 1 

except IndexError: 

pass 

d.append(left + right / m) 

return d 

 

 

""" 

functions used in the fitting procedure, with initial guesses 

""" 

 

 

def print_and_raise_error(xs, ys, name): 

print('Index error in', name) 

print('ys: ', ys) 

print('xs: ', xs) 

raise RuntimeError 

 

 

def reciprocal(x, a, b, n): 

""" 

reciprocal function to the power n to fit convergence data 

""" 

if n < 1: 

n = 1 

elif n > 5: 

n = 5 

if isinstance(x, list): 

y_l = [] 

for x_v in x: 

y_l.append(a + b / x_v ** n) 

y = np.array(y_l) 

else: 

y = a + b / x ** n 

return y 

 

 

def p0_reciprocal(xs, ys): 

""" 

predictor for first guess for reciprocal 

""" 

a0 = ys[len(ys) - 1] 

b0 = ys[0]*xs[0] - a0*xs[0] 

return [a0, b0, 1] 

 

 

def exponential(x, a, b, n): 

""" 

exponential function base n to fit convergence data 

""" 

if n < 1.000001: 

n = 1.000001 

elif n > 1.2: 

n = 1.2 

if b < -10: 

b = -10 

elif b > 10: 

b = 10 

if isinstance(x, list): 

y_l = [] 

for x_v in x: 

y_l.append(a + b * n ** -x_v) 

y = np.array(y_l) 

else: 

y = a + b * n ** -x 

return y 

 

 

def p0_exponential(xs, ys): 

n0 = 1.005 

b0 = (n0 ** -xs[-1] - n0 ** -xs[1]) / (ys[-1] - ys[1]) 

a0 = ys[1] - b0 * n0 ** -xs[1] 

#a0 = ys[-1] 

#b0 = (ys[0] - a0) / n0 ** xs[0] 

return [a0, b0, n0] 

 

 

def single_reciprocal(x, a, b, c): 

""" 

reciprocal function to fit convergence data 

""" 

if isinstance(x, list): 

y_l = [] 

for x_v in x: 

y_l.append(a + b / (x_v - c)) 

y = np.array(y_l) 

else: 

y = a + b / (x - c) 

return y 

 

 

def p0_single_reciprocal(xs, ys): 

c = 1 

b = (1/(xs[-1] - c)-1/(xs[1] - c)) / (ys[-1] - ys[1]) 

a = ys[1] - b / (xs[1] - c) 

return [a, b, c] 

 

 

def simple_reciprocal(x, a, b): 

""" 

reciprocal function to fit convergence data 

""" 

if isinstance(x, list): 

y_l = [] 

for x_v in x: 

y_l.append(a + b / x_v) 

y = np.array(y_l) 

else: 

y = a + b / x 

return y 

 

 

def p0_simple_reciprocal(xs, ys): 

#b = (ys[-1] - ys[1]) / (1/xs[-1] - 1/xs[1]) 

#a = ys[1] - b / xs[1] 

b = (ys[-1] - ys[-2]) / (1/(xs[-1]) - 1/(xs[-2])) 

a = ys[-2] - b / (xs[-2]) 

return [a, b] 

 

 

def simple_2reciprocal(x, a, b): 

""" 

reciprocal function to fit convergence data 

""" 

c = 2 

if isinstance(x, list): 

y_l = [] 

for x_v in x: 

y_l.append(a + b / x_v ** c) 

y = np.array(y_l) 

else: 

y = a + b / x ** c 

return y 

 

 

def p0_simple_2reciprocal(xs, ys): 

c = 2 

b = (ys[-1] - ys[1]) / (1/xs[-1]**c - 1/xs[1]**c) 

a = ys[1] - b / xs[1]**c 

return [a, b] 

 

 

def simple_4reciprocal(x, a, b): 

""" 

reciprocal function to fit convergence data 

""" 

c = 4 

if isinstance(x, list): 

y_l = [] 

for x_v in x: 

y_l.append(a + b / x_v ** c) 

y = np.array(y_l) 

else: 

y = a + b / x ** c 

return y 

 

 

def p0_simple_4reciprocal(xs, ys): 

c = 4 

b = (ys[-1] - ys[1]) / (1/xs[-1]**c - 1/xs[1]**c) 

a = ys[1] - b / xs[1]**c 

return [a, b] 

 

 

def simple_5reciprocal(x, a, b): 

""" 

reciprocal function to fit convergence data 

""" 

c = 0.5 

if isinstance(x, list): 

y_l = [] 

for x_v in x: 

y_l.append(a + b / x_v ** c) 

y = np.array(y_l) 

else: 

y = a + b / x ** c 

return y 

 

 

def p0_simple_5reciprocal(xs, ys): 

c = 0.5 

b = (ys[-1] - ys[1]) / (1/xs[-1]**c - 1/xs[1]**c) 

a = ys[1] - b / xs[1]**c 

return [a, b] 

 

 

def extrapolate_simple_reciprocal(xs, ys): 

b = (ys[-2] - ys[-1]) / (1/(xs[-2]) - 1/(xs[-1])) 

a = ys[-1] - b / (xs[-1]) 

return [a, b] 

 

 

def extrapolate_reciprocal(xs, ys, n, noise): 

""" 

return the parameters such that a + b / x^n hits the last two data points 

""" 

if len(xs) > 4 and noise: 

y1 = (ys[-3] + ys[-4]) / 2 

y2 = (ys[-1] + ys[-2]) / 2 

x1 = (xs[-3] + xs[-4]) / 2 

x2 = (xs[-1] + xs[-2]) / 2 

try: 

b = (y1 - y2) / (1/x1**n - 1/x2**n) 

a = y2 - b / x2**n 

except IndexError: 

print_and_raise_error(xs, ys, 'extrapolate_reciprocal') 

else: 

try: 

b = (ys[-2] - ys[-1]) / (1/(xs[-2])**n - 1/(xs[-1])**n) 

a = ys[-1] - b / (xs[-1])**n 

except IndexError: 

print_and_raise_error(xs, ys, 'extrapolate_reciprocal') 

return [a, b, n] 

 

 

def measure(function, xs, ys, popt, weights): 

""" 

measure the quality of a fit 

""" 

m = 0 

n = 0 

for x in xs: 

if len(popt) == 2: 

m += (ys[n] - function(x, popt[0], popt[1]))**2 * weights[n] 

elif len(popt) == 3: 

m += (ys[n] - function(x, popt[0], popt[1], popt[2]))**2 * weights[n] 

else: 

raise NotImplementedError 

n += 1 

return m 

 

 

def get_weights(xs, ys, mode=2): 

ds = get_derivatives(xs, ys, fd=True) 

if mode == 1: 

mind = np.inf 

for d in ds: 

mind = min(abs(d), mind) 

weights = [] 

for d in ds: 

weights.append(abs((mind / d))) 

if mode == 2: 

maxxs = max(xs)**2 

weights = [] 

for x in xs: 

weights.append(x**2 / maxxs) 

else: 

weights = [1] * len(xs) 

return weights 

 

 

def multi_curve_fit(xs, ys, verbose): 

""" 

fit multiple functions to the x, y data, return the best fit 

""" 

#functions = {exponential: p0_exponential, reciprocal: p0_reciprocal, single_reciprocal: p0_single_reciprocal} 

functions = { 

exponential: p0_exponential, 

reciprocal: p0_reciprocal, 

#single_reciprocal: p0_single_reciprocal, 

simple_reciprocal: p0_simple_reciprocal, 

simple_2reciprocal: p0_simple_2reciprocal, 

simple_4reciprocal: p0_simple_4reciprocal, 

simple_5reciprocal: p0_simple_5reciprocal 

} 

from scipy.optimize import curve_fit 

fit_results = {} 

best = ['', np.inf] 

for function in functions: 

try: 

weights = get_weights(xs, ys) 

popt, pcov = curve_fit(function, xs, ys, functions[function](xs, ys), maxfev=8000, sigma=weights) 

pcov = [] 

m = measure(function, xs, ys, popt, weights) 

fit_results.update({function: {'measure': m, 'popt': popt, 'pcov': pcov}}) 

for f in fit_results: 

if fit_results[f]['measure'] <= best[1]: 

best = f, fit_results[f]['measure'] 

if verbose: 

print(str(function), m) 

except RuntimeError: 

print('no fit found for ', function) 

 

return fit_results[best[0]]['popt'], fit_results[best[0]]['pcov'], best 

 

 

def multi_reciprocal_extra(xs, ys, noise=False): 

""" 

Calculates for a series of powers ns the parameters for which the last two points are at the curve. 

With these parameters measure how well the other data points fit. 

return the best fit. 

""" 

ns = np.linspace(0.5, 6.0, num=56) 

best = ['', np.inf] 

fit_results = {} 

weights = get_weights(xs, ys) 

for n in ns: 

popt = extrapolate_reciprocal(xs, ys, n, noise) 

m = measure(reciprocal, xs, ys, popt, weights) 

pcov = [] 

fit_results.update({n: {'measure': m, 'popt': popt, 'pcov': pcov}}) 

for n in fit_results: 

if fit_results[n]['measure'] <= best[1]: 

best = reciprocal, fit_results[n]['measure'], n 

return fit_results[best[2]]['popt'], fit_results[best[2]]['pcov'], best 

 

 

def print_plot_line(function, popt, xs, ys, name, tol=0.05, extra=''): 

""" 

print the gnuplot command line to plot the x, y data with the fitted function using the popt parameters 

""" 

idp = id_generator() 

f = open('convdat.'+str(idp), mode='w') 

for n in range(0, len(ys), 1): 

f.write(str(xs[n]) + ' ' + str(ys[n]) + '\n') 

f.close() 

tol = abs(tol) 

line = "plot 'convdat.%s' pointsize 4 lt 0, " % idp 

line += '%s lt 3, %s lt 4, %s lt 4, ' % (popt[0], popt[0] - tol, popt[0] + tol) 

if function is exponential: 

line += "%s + %s * %s ** -x" % (popt[0], popt[1], min(max(1.00001, popt[2]), 1.2)) 

elif function is reciprocal: 

line += "%s + %s / x**%s" % (popt[0], popt[1], min(max(0.5, popt[2]), 6)) 

elif function is single_reciprocal: 

line += "%s + %s / (x - %s)" % (popt[0], popt[1], popt[2]) 

elif function is simple_reciprocal: 

line += "%s + %s / x" % (popt[0], popt[1]) 

elif function is simple_2reciprocal: 

line += "%s + %s / x**2" % (popt[0], popt[1]) 

elif function is simple_4reciprocal: 

line += "%s + %s / x**4" % (popt[0], popt[1]) 

elif function is simple_5reciprocal: 

line += "%s + %s / x**0.5" % (popt[0], popt[1]) 

else: 

print(function, ' no plot ') 

 

with open('plot-fits', mode='a') as f: 

f.write('set title "' + name + ' - ' + extra + '"\n') 

f.write("set output '" + name + '-' + idp + ".gif'" + '\n') 

f.write("set yrange [" + str(popt[0] - 5 * tol) + ':' + str(popt[0] + 5 * tol)+']\n') 

f.write(line + '\n') 

f.write('pause -1 \n') 

 

 

def determine_convergence(xs, ys, name, tol=0.0001, extra='', verbose=False, mode='extra', plots=True): 

""" 

test it and at which x_value dy(x)/dx < tol for all x >= x_value, conv is true is such a x_value exists. 

""" 

conv = False 

x_value = float('inf') 

y_value = None 

n_value = None 

popt = [None, None, None] 

if len(xs) > 2: 

ds = get_derivatives(xs[0:len(ys)], ys) 

try: 

if None not in ys: 

if mode == 'fit': 

popt, pcov, func = multi_curve_fit(xs, ys, verbose) 

elif mode == 'extra': 

popt, pcov, func = multi_reciprocal_extra(xs, ys) 

elif mode == 'extra_noise': 

popt, pcov, func = multi_reciprocal_extra(xs, ys, noise=True) 

else: 

raise NotImplementedError('nknown mode for test conv') 

if func[1] > abs(tol): 

print('warning function ', func[0], ' as the best fit but not a good fit: ', func[1]) 

# todo print this to file via a method in helper, as dict 

if plots: 

with open(name+'.fitdat', mode='a') as f: 

f.write('{') 

f.write('"popt": ' + str(popt) + ', ') 

f.write('"pcov": ' + str(pcov) + ', ') 

f.write('"data": [') 

for n in range(0, len(ys), 1): 

f.write('[' + str(xs[n]) + ' ' + str(ys[n]) + ']') 

f.write(']}\n') 

 

print_plot_line(func[0], popt, xs, ys, name, tol=tol, extra=extra) 

 

except ImportError: 

popt, pcov = None, None 

for n in range(0, len(ds), 1): 

if verbose: 

print(n, ys[n]) 

print(ys) 

if tol < 0: 

if popt[0] is not None: 

test = abs(popt[0] - ys[n]) 

else: 

test = float('inf') 

else: 

test = abs(ds[n]) 

if verbose: 

print(test) 

if test < abs(tol): 

if verbose: 

print('converged') 

conv = True 

if xs[n] < x_value: 

x_value = xs[n] 

y_value = ys[n] 

n_value = n 

else: 

if verbose: 

print('not converged') 

conv = False 

x_value = float('inf') 

if n_value is None: 

return [conv, x_value, y_value, n_value, popt[0], None] 

else: 

return [conv, x_value, y_value, n_value, popt[0], ds[n_value]] 

else: 

return [conv, x_value, y_value, n_value, popt[0], None]