-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
37 lines (28 loc) · 1.07 KB
/
utils.py
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
import torch
import numpy as np
from scipy.signal import butter, sosfiltfilt
def build_model():
"""Gets the trained PyTorch TorchScript model"""
model = torch.jit.load('model.pt')
return model
def preprocess(pressure, compliance):
"""Pre-processes tympanogram data"""
if np.any(np.diff(pressure) <= 0):
raise ValueError('Pressure sweep is not monotonic')
default_p = np.arange(-399, 201, dtype=float)
lpf = butter(4, 0.04, 'lowpass', output='sos')
trace = np.interp(default_p, pressure, compliance)
trace = np.stack([trace, sosfiltfilt(lpf, trace)])
trace = torch.from_numpy(trace).float()[None]
return trace
def sim_tracing(tpp=0, ecv=1.0, sa=1.0, zeta=2e-3, slope=5e-4):
"""An analytic formula for generating simulated tympanograms"""
p = np.linspace(-399, 200, 600)
atm = 1e5 / 10 # 1 atm in decaPascals
a = 1 / (1 + (tpp - p)**2 / (zeta**2 * (2*atm + tpp + p)**2))
a200 = a[-1]
amax = sa / (1 - a200)
a *= amax
a += ecv - amax * a200
a += slope * (p - tpp) * (p < tpp)
return p, a