Smoothing matrix stored in memory instead of file
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Casper Jansen 2023-01-19 16:58:26 +01:00
parent b19c5ad38e
commit afdec26d49

View File

@ -34,11 +34,8 @@ __all__ = ['SmoothingType', 'smoothSpectralData', 'SmoothingWidth']
from enum import Enum, unique from enum import Enum, unique
import bisect import bisect
import codecs
import copy import copy
import json
import numpy as np import numpy as np
import os
@unique @unique
@ -206,29 +203,20 @@ def smoothSpectralData(freq, M, sw: SmoothingWidth,
Psm[0] = P[0] # Reuse old value in case first data.. Psm[0] = P[0] # Reuse old value in case first data..
# ..point is skipped. Not plotted any way. # ..point is skipped. Not plotted any way.
# Find smoothing matrix. Look it up, otherwise calculate and store. # Re-use smoothing matrix Q if available. Otherwise, calculate.
# TODO: find a way to reduce file size, loading time, a good place to store # Store in dict 'Qdict'
# TODO: also save the last table in memory
fname = 'smoothing_tables.json' # storage file
nfft = int(2*(len(freq)-1)) nfft = int(2*(len(freq)-1))
key = f"nfft{nfft}_Noct{Noct}" # name key = f"nfft{nfft}_Noct{Noct}" # matrix name
if os.path.isfile(fname):
with open(fname) as f: if 'Qdict' not in globals(): # Guarantee Qdict exists
Qdict = json.load(f) global Qdict
else: Qdict = {}
Qdict = {'Help': 'This file contains smoothing tables'}
json.dump(Qdict, codecs.open(fname, 'w', encoding='utf-8'),
separators=(',', ':'), sort_keys=True, indent=4)
if key in Qdict: if key in Qdict:
# Load = fast Q = Qdict[key]
Q = np.asarray(Qdict[key])
else: else:
# Calculate new matrix; store
Q = smoothCalcMatrix(freq, sw) Q = smoothCalcMatrix(freq, sw)
Qdict[key] = Q.tolist() # json cannot handle ndarray Qdict[key] = Q
json.dump(Qdict, codecs.open(fname, 'w', encoding='utf-8'),
separators=(',', ':'), sort_keys=True, indent=4)
# Apply smoothing # Apply smoothing
Psm = np.matmul(Q, P) Psm = np.matmul(Q, P)