Skip to content

Commit 6a4f275

Browse files
committed
Square up caching code to make it easier to read. Room for improvement
1 parent f4a0863 commit 6a4f275

5 files changed

Lines changed: 34 additions & 24 deletions

File tree

.github/workflows/main.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ jobs:
8383
shell: bash -l {0}
8484
run: |
8585
conda activate mhkit-env
86-
pip install -e ".[all,dev]"
86+
pip install -e ".[all,dev]"
8787
8888
- name: Prepare non-hindcast API data
8989
shell: bash -l {0}
@@ -134,7 +134,7 @@ jobs:
134134
shell: bash -l {0}
135135
run: |
136136
conda activate mhkit-env
137-
pip install -e ".[all,dev]"
137+
pip install -e ".[all,dev]"
138138
139139
- name: Prepare Wave Hindcast data
140140
shell: bash -l {0}
@@ -183,7 +183,7 @@ jobs:
183183
shell: bash -l {0}
184184
run: |
185185
conda activate mhkit-env
186-
pip install -e ".[all,dev]"
186+
pip install -e ".[all,dev]"
187187
188188
- name: Prepare Wind Hindcast data
189189
shell: bash -l {0}
@@ -290,7 +290,7 @@ jobs:
290290
shell: bash -l {0}
291291
run: |
292292
python -m pip install --upgrade pip wheel
293-
pip install -e ".[all,dev]"
293+
pip install -e ".[all,dev]"
294294
295295
- name: Reinstall h5py and netCDF4 with system libraries
296296
if: runner.os == 'Linux'
@@ -362,7 +362,7 @@ jobs:
362362
shell: bash -l {0}
363363
run: |
364364
conda activate mhkit-env
365-
pip install -e ".[all,dev]" --no-deps
365+
pip install -e ".[all,dev]" --no-deps
366366
367367
- name: Download Wave Hindcast data from artifact
368368
uses: actions/download-artifact@v4
@@ -514,7 +514,7 @@ jobs:
514514
needs.prepare-nonhindcast-cache.result == 'success' &&
515515
needs.prepare-wave-hindcast-cache.result == 'skipped' &&
516516
needs.prepare-wind-hindcast-cache.result == 'skipped' &&
517-
needs.check-changes.outputs.should-run-hindcast == 'false'
517+
needs.check-changes.outputs.should-run-hindcast == 'false'
518518
) ||
519519
(
520520
needs.prepare-nonhindcast-cache.result == 'success' &&
@@ -584,7 +584,7 @@ jobs:
584584
shell: bash -l {0}
585585
run: |
586586
conda activate mhkit-env
587-
pip install -e ".[all,dev]" --no-deps
587+
pip install -e ".[all,dev]" --no-deps
588588
589589
- name: Download non-hindcast data
590590
uses: actions/download-artifact@v4

mhkit/tests/wave/io/hindcast/test_wind_toolkit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def setUpClass(self):
4040
index_col="time_index",
4141
names=["time_index", "windspeed_10m_0", "windspeed_10m_1"],
4242
header=0,
43-
dtype={"windspeed_10m_0": "float32", "windspeed_10m_1": "float32"},
43+
dtype={"windspeed_10m_0": "float64", "windspeed_10m_1": "float64"},
4444
)
4545
self.ml.index = pd.to_datetime(self.ml.index)
4646

mhkit/tests/wave/io/test_cdip.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ def test_request_parse_workflow_multiyear(self):
128128
expected_index0 = datetime(year1, 1, 1)
129129
expected_index_final = datetime(year2, 12, 31)
130130

131-
wave1D = data["data"]["wave"]
131+
wave1D = data["wave"]
132132
self.assertEqual(wave1D.index[0].floor("D").to_pydatetime(), expected_index0)
133133

134134
self.assertEqual(
135135
wave1D.index[-1].floor("D").to_pydatetime(), expected_index_final
136136
)
137137

138-
for key, wave2D in data["data"]["wave2D"].items():
138+
for key, wave2D in data["wave2D"].items():
139139
self.assertEqual(
140140
wave2D.index[0].floor("D").to_pydatetime(), expected_index0
141141
)
@@ -158,7 +158,7 @@ def test_plot_boxplot(self):
158158
)
159159

160160
plt.figure()
161-
wave.graphics.plot_boxplot(data["data"]["wave"]["waveHs"])
161+
wave.graphics.plot_boxplot(data["wave"]["waveHs"])
162162
plt.savefig(filename, format="png")
163163
plt.close()
164164

@@ -181,9 +181,9 @@ def test_plot_compendium(self):
181181

182182
plt.figure()
183183
wave.graphics.plot_compendium(
184-
data["data"]["wave"]["waveHs"],
185-
data["data"]["wave"]["waveTp"],
186-
data["data"]["wave"]["waveDp"],
184+
data["wave"]["waveHs"],
185+
data["wave"]["waveTp"],
186+
data["wave"]["waveDp"],
187187
)
188188
plt.savefig(filename, format="png")
189189
plt.close()

mhkit/utils/cache.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,18 @@ def _load_cache(file_extension, cache_filepath):
119119
)
120120
elif file_extension == ".pkl":
121121
with open(cache_filepath, "rb") as f:
122-
data = pickle.load(f) # dictionary with 'data' and 'metadata' keys
123-
124-
return data
122+
data_dict = pickle.load(f) # some format of 'data' and 'metadata'
123+
# TODO Must be a better way to handle these if we are writing the cache.
124+
if isinstance(data_dict, tuple):
125+
# CDIP call returns dictionary in size 1 tuple
126+
if len(data_dict) == 1 and isinstance(data_dict[0], dict):
127+
data_dict = data_dict[0]
128+
# WPTO hindcast call returns size 2 tuple
129+
if len(data_dict) == 2:
130+
return data_dict[0], data_dict[1]
131+
data, metadata = data_dict["data"], data_dict["metadata"]
132+
133+
return data, metadata
125134

126135
def _write_cache(data, metadata, file_extension, cache_filepath):
127136
"""Store data in the cache file based on the extension."""
@@ -154,7 +163,8 @@ def _write_cache(data, metadata, file_extension, cache_filepath):
154163
if os.path.isfile(cache_filepath) and (
155164
cache_content is None or cache_content["data"] is None
156165
):
157-
return _load_cache(file_extension, cache_filepath) + (cache_filepath,)
166+
data, metadata = _load_cache(file_extension, cache_filepath)
167+
return data, metadata, cache_filepath
158168

159169
# Store data in cache if provided
160170
if cache_content and cache_content["data"] is not None:

mhkit/wave/io/cdip.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,20 +378,20 @@ def request_parse_workflow(
378378
"write_json": None,
379379
},
380380
)
381-
multiyear_data[year] = year_data["data"]
381+
multiyear_data[year] = year_data
382382

383-
for data_key in year_data["data"].keys():
383+
for data_key in year_data.keys():
384384
if data_key.endswith("2D"):
385-
data["data"][data_key] = {}
386-
for data_key2D in year_data["data"][data_key].keys():
385+
data[data_key] = {}
386+
for data_key2D in year_data[data_key].keys():
387387
data_list = []
388388
for year in years:
389389
data2D = multiyear_data[year][data_key][data_key2D]
390390
data_list.append(data2D)
391-
data["data"][data_key][data_key2D] = pd.concat(data_list)
391+
data[data_key][data_key2D] = pd.concat(data_list)
392392
else:
393393
data_list = [multiyear_data[year][data_key] for year in years]
394-
data["data"][data_key] = pd.concat(data_list)
394+
data[data_key] = pd.concat(data_list)
395395

396396
if buoy_name:
397397
try:

0 commit comments

Comments
 (0)