|
| 1 | +from . import (runAmiciSimulation, SensitivityOrder_none, |
| 2 | + SensitivityMethod_forward) |
| 3 | +import numpy as np |
| 4 | +import copy |
| 5 | + |
| 6 | + |
| 7 | +def check_finite_difference(x0, model, solver, edata, ip, fields, |
| 8 | + assert_fun, atol=1e-4, rtol=1e-4, epsilon=1e-3): |
| 9 | + old_sensitivity_order = solver.getSensitivityOrder() |
| 10 | + old_parameters = model.getParameters() |
| 11 | + old_plist = model.getParameterList() |
| 12 | + |
| 13 | + # sensitivity |
| 14 | + p = copy.deepcopy(x0) |
| 15 | + plist = [ip] |
| 16 | + |
| 17 | + model.setParameters(p) |
| 18 | + model.setParameterList(plist) |
| 19 | + rdata = runAmiciSimulation(model, solver, edata) |
| 20 | + |
| 21 | + # finite difference |
| 22 | + solver.setSensitivityOrder(SensitivityOrder_none) |
| 23 | + |
| 24 | + # forward: |
| 25 | + p = copy.deepcopy(x0) |
| 26 | + p[ip] += epsilon/2 |
| 27 | + model.setParameters(p) |
| 28 | + rdataf = runAmiciSimulation(model, solver, edata) |
| 29 | + |
| 30 | + # backward: |
| 31 | + p = copy.deepcopy(x0) |
| 32 | + p[ip] -= epsilon/2 |
| 33 | + model.setParameters(p) |
| 34 | + rdatab = runAmiciSimulation(model, solver, edata) |
| 35 | + |
| 36 | + for field in fields: |
| 37 | + sensi_raw = rdata[f's{field}'] |
| 38 | + fd = (rdataf[field]-rdatab[field])/epsilon |
| 39 | + if len(sensi_raw.shape) == 1: |
| 40 | + sensi = sensi_raw[0] |
| 41 | + elif len(sensi_raw.shape) == 2: |
| 42 | + sensi = sensi_raw[:, 0] |
| 43 | + elif len(sensi_raw.shape) == 3: |
| 44 | + sensi = sensi_raw[:, 0, :] |
| 45 | + else: |
| 46 | + assert_fun(False) # not implemented |
| 47 | + |
| 48 | + check_close(sensi, fd, assert_fun, atol, rtol, field, ip=ip) |
| 49 | + |
| 50 | + solver.setSensitivityOrder(old_sensitivity_order) |
| 51 | + model.setParameters(old_parameters) |
| 52 | + model.setParameterList(old_plist) |
| 53 | + |
| 54 | + |
| 55 | +def check_derivatives(model, solver, edata, assert_fun, |
| 56 | + atol=1e-4, rtol=1e-4, epsilon=1e-3): |
| 57 | + """Finite differences check for likelihood gradient |
| 58 | +
|
| 59 | + Arguments: |
| 60 | + model: amici model |
| 61 | + solver: amici solver |
| 62 | + edata: exp data |
| 63 | + atol: absolute tolerance |
| 64 | + rtol: relative tolerance |
| 65 | + epsilon: finite difference step-size |
| 66 | + """ |
| 67 | + from scipy.optimize import check_grad |
| 68 | + |
| 69 | + p = np.array(model.getParameters()) |
| 70 | + |
| 71 | + rdata = runAmiciSimulation(model, solver, edata) |
| 72 | + |
| 73 | + fields = ['llh'] |
| 74 | + |
| 75 | + leastsquares_applicable = \ |
| 76 | + solver.getSensitivityMethod() == SensitivityMethod_forward |
| 77 | + |
| 78 | + if 'ssigmay' in rdata.keys(): |
| 79 | + if rdata['ssigmay'] is not None: |
| 80 | + if rdata['ssigmay'].any(): |
| 81 | + leastsquares_applicable = False |
| 82 | + |
| 83 | + if leastsquares_applicable: |
| 84 | + fields += ['res', 'x', 'y'] |
| 85 | + |
| 86 | + check_results(rdata, 'FIM', |
| 87 | + np.dot(rdata['sres'].transpose(), rdata['sres']), |
| 88 | + assert_fun, |
| 89 | + 1e-8, 1e-4) |
| 90 | + check_results(rdata, 'sllh', |
| 91 | + -np.dot(rdata['res'].transpose(), rdata['sres']), |
| 92 | + assert_fun, |
| 93 | + 1e-8, 1e-4) |
| 94 | + for ip in range(len(p)): |
| 95 | + check_finite_difference(p, model, solver, edata, ip, fields, |
| 96 | + assert_fun, atol=atol, rtol=rtol, |
| 97 | + epsilon=epsilon) |
| 98 | + |
| 99 | + |
| 100 | +def check_close(result, expected, assert_fun, atol, rtol, field, ip=None): |
| 101 | + close = np.isclose(result, expected, atol=atol, rtol=rtol, equal_nan=True) |
| 102 | + |
| 103 | + if not close.all(): |
| 104 | + if ip is None: |
| 105 | + index_str = '' |
| 106 | + check_type = 'Regression check ' |
| 107 | + else: |
| 108 | + index_str = f'at index ip={ip} ' |
| 109 | + check_type = 'FD check ' |
| 110 | + print(f'{check_type} failed for {field} {index_str}for ' |
| 111 | + f'{close.sum()} indices:') |
| 112 | + adev = np.abs(result - expected) |
| 113 | + rdev = np.abs((result - expected)/(expected + atol)) |
| 114 | + print(f'max(adev): {adev.max()}, max(rdev): {rdev.max()}') |
| 115 | + |
| 116 | + assert_fun(close.all()) |
| 117 | + |
| 118 | + |
| 119 | +def check_results(rdata, field, expected, assert_fun, atol, rtol): |
| 120 | + """ |
| 121 | + checks whether rdata[field] agrees with expected according to provided |
| 122 | + tolerances |
| 123 | +
|
| 124 | + Arguments: |
| 125 | + rdata: simulation results as returned by amici.runAmiciSimulation |
| 126 | + field: name of the field to check |
| 127 | + expected: expected test results |
| 128 | + atol: absolute tolerance |
| 129 | + rtol: relative tolerance |
| 130 | + """ |
| 131 | + |
| 132 | + result = rdata[field] |
| 133 | + if type(result) is float: |
| 134 | + result = np.array(result) |
| 135 | + |
| 136 | + check_close(result, expected, assert_fun, atol, rtol, field) |
| 137 | + |
0 commit comments