You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From line_profiler@wholmgren, you stated that loading the matlab file was one of the two bottlenecks.
Loading the file is pretty slow, and we are doing it each time we run the function. Could we cache the file in the module to avoid reloading it?
There are actually two ways to see that:
Loading the file when we first call lookup_linke_turbidity
CACHE= {}
[...]
deflookup_linke_turbidity(...):
[...]
# try to get `LinkeTurbidity` data from the `CACHE`mat=CACHE.get('LinkeTurbidity')
ifnotmat:
# load `LinkeTurbidity` data and save then to the `CACHE`mat=scipy.io.loadmat(filepath)
CACHE['LinkeTurbidity'] =mat
[...]
Loading the file at import time, and cache it
CACHE= {}
defload_linke_turbidity():
# scipy import try except + filepathCACHE['LinkeTurbidity'] =scipy.io.loadmat(filepath)
load_linke_turbidity()
[...]
deflookup_linke_turbidity(...):
[...]
# try to get `LinkeTurbidity` data from the `CACHE`mat=CACHE.get('LinkeTurbidity')
[...]
Maybe... A cache sounds like something that might be easy to implement initially but might also have all sorts of unexpected side effects. This is probably more of a concern in low-memory and parallel environments (I know some people use pvlib in these situations).
Is it slow because it's a matlab file? Would a h5 file be faster? Would support for a vectorized lookup be a better approach? Could you preload the turbidity data? Maybe joblib or similar could handle the caching for you?
I'm curious if breaking the single MAT file into smaller chunks (e.g. 18lats X 36lons = 648 mat files) each containing a 10deg x 10deg set of data would be a simple imrpovement. The load function would include lat/lon, and only one smaller file would be opened.
In Matlab pvlib we read this file the first time that a Linke turbidity is needed, and hold the data in memory. Reading it each time a value is needed killed performance.
If we want to see if the file format is the bottleneck, I can export the matlab file as hdf5.
compression_opts set to 9 (the maximum) give a better compression than the matlab file (~13,600 KB Vs ~19,900 KB).
I used tables for reading the h5 file as it is what pandas is using. It looks like it is an optional dependency though, so it won't come with pandas and will need to be installed by users (a little like what pvlib is doing with scipy.io today).
That's great! I think an optional tables dependency is the right way to go. tables is widely used and it's a smaller dependency than scipy.
You might experiment with the compression_opts value. I find that 4 or 5 is usually the sweet spot for speed vs. file size, but it depends on the data.
Activity
wholmgren commentedon Mar 14, 2018
Maybe... A cache sounds like something that might be easy to implement initially but might also have all sorts of unexpected side effects. This is probably more of a concern in low-memory and parallel environments (I know some people use pvlib in these situations).
Is it slow because it's a matlab file? Would a h5 file be faster? Would support for a vectorized lookup be a better approach? Could you preload the turbidity data? Maybe joblib or similar could handle the caching for you?
SunPowerMike commentedon Mar 14, 2018
I'm curious if breaking the single MAT file into smaller chunks (e.g. 18lats X 36lons = 648 mat files) each containing a 10deg x 10deg set of data would be a simple imrpovement. The load function would include lat/lon, and only one smaller file would be opened.
wholmgren commentedon Mar 15, 2018
Chunking might help. hdf5 supports chunking within a single file.
adriesse commentedon Mar 15, 2018
Or a memory-mapped numpy array:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.memmap.html#numpy.memmap
cwhanse commentedon Mar 15, 2018
In Matlab pvlib we read this file the first time that a Linke turbidity is needed, and hold the data in memory. Reading it each time a value is needed killed performance.
If we want to see if the file format is the bottleneck, I can export the matlab file as hdf5.
cedricleroy commentedon Mar 27, 2018
Here is what I have trying
hdf5:Here is what I used for creating the
h5file:compression_optsset to 9 (the maximum) give a better compression than the matlab file (~13,600 KB Vs ~19,900 KB).I used
tablesfor reading theh5file as it is whatpandasis using. It looks like it is an optional dependency though, so it won't come withpandasand will need to be installed by users (a little like what pvlib is doing withscipy.iotoday).wholmgren commentedon Mar 27, 2018
That's great! I think an optional tables dependency is the right way to go. tables is widely used and it's a smaller dependency than scipy.
You might experiment with the compression_opts value. I find that 4 or 5 is usually the sweet spot for speed vs. file size, but it depends on the data.
Can you make a pull request for this improvement?
wholmgren commentedon Apr 6, 2018
closed by #442.