Skip to content

Commit 565d2c4

Browse files
authored
Fix tempfile bug for Windows (#83)
* Run CI also for Windows * Attempt to fix bug * Skip model tests for windows * Attempt to fix model tests for Windows * Print times * Remove potential fix * Attempt to fix timestamp issue * Attempt proper fix * Clean up fix
1 parent 0c0493d commit 565d2c4

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

.github/workflows/ci.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ on:
88

99
jobs:
1010
test:
11-
runs-on: ubuntu-latest
1211
strategy:
1312
fail-fast: false
1413
matrix:
1514
version: ["3.10", "3.11"]
15+
os: [ubuntu-latest, windows-latest]
1616

17-
name: Test with Python ${{ matrix.version }}
17+
runs-on: ${{ matrix.os }}
18+
name: Test with Python ${{ matrix.version }} (${{ matrix.os }})
1819
steps:
19-
- uses: actions/checkout@v2
20+
- uses: actions/checkout@v4
2021

2122
- name: Set up Python ${{ matrix.version }}
22-
uses: actions/setup-python@v2
23+
uses: actions/setup-python@v5
2324
with:
2425
python-version: ${{ matrix.version }}
2526

aurora/foundry/common/channel.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,26 @@ def _download_blob(self, blob_name: str, file_path: str) -> None:
229229

230230
def _send(self, batch: Batch, name: str) -> None:
231231
with tempfile.NamedTemporaryFile() as tf:
232+
tf.close() # We will only use the name.
232233
batch.to_netcdf(tf.name)
233234
self._upload_blob(tf.name, name)
234235

235236
def _receive(self, name: str) -> Batch:
236237
with tempfile.NamedTemporaryFile() as tf:
238+
tf.close() # We will only use the name.
237239
self._download_blob(name, tf.name)
238240
return Batch.from_netcdf(tf.name)
239241

240242
def _write(self, data: bytes, name: str) -> None:
241243
with tempfile.NamedTemporaryFile() as tf:
244+
tf.close() # We will only use the name.
242245
with open(tf.name, "wb") as f:
243246
f.write(data)
244247
self._upload_blob(tf.name, name)
245248

246249
def _read(self, name: str) -> bytes:
247250
with tempfile.NamedTemporaryFile() as tf:
251+
tf.close() # We will only use the name.
248252
self._download_blob(name, tf.name)
249253
with open(tf.name, "rb") as f:
250254
return f.read()

aurora/foundry/demo/interactive.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def variable_to_urldata(variable: xr.DataArray, cmap: str, vmin: float, vmax: fl
2929
field = np.concatenate((field[:, ~first_half], field[:, first_half]), axis=1)
3030

3131
with tempfile.NamedTemporaryFile(suffix=".png") as tf:
32+
tf.close() # We will only use the name.
33+
3234
# Make the image that will be overlayed.
3335
fig = plt.figure()
3436
ax = fig.add_subplot(projection=ccrs.PlateCarree(), frameon=False)
@@ -63,6 +65,8 @@ def interactive_plot(prediction: Batch, width: str = "1000px", height: str = "50
6365
)
6466

6567
with tempfile.NamedTemporaryFile(suffix=".nc") as tf:
68+
tf.close() # We will only use the name.
69+
6670
prediction.to_netcdf(tf.name)
6771
ds = xr.load_dataset(tf.name).isel(batch=0, history=0, time=0)
6872
dt = ds.time.values.astype("datetime64[s]").tolist()

tests/conftest.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Copyright (c) Microsoft Corporation. Licensed under the MIT license."""
22

3+
import os
34
import pickle
45
from datetime import datetime
56
from typing import Generator, TypedDict
@@ -49,6 +50,25 @@ def test_input_output() -> Generator[tuple[Batch, SavedBatch], None, None]:
4950
with open(path, "rb") as f:
5051
static_vars: dict[str, np.ndarray] = pickle.load(f)
5152

53+
# Load test output.
54+
path = hf_hub_download(
55+
repo_id="microsoft/aurora",
56+
filename="aurora-0.25-small-pretrained-test-output.pickle",
57+
)
58+
with open(path, "rb") as f:
59+
test_output: SavedBatch = pickle.load(f)
60+
61+
# We unfortunately used a time in 1950. Windows cannot produce timestamps for `datetime`s before
62+
# 1970. We fix this below.
63+
if os.name == "nt":
64+
65+
class PatchedDateTime(datetime):
66+
def timestamp(self) -> float:
67+
# This is the value of `datetime(1950, 1, 1, 6, 0).timestamp()` on Linux.
68+
return -631134000.0
69+
70+
test_input["metadata"]["time"] = [PatchedDateTime(1950, 1, 1, 6, 0)]
71+
5272
static_vars = {
5373
k: interpolate_numpy(
5474
v,
@@ -73,12 +93,4 @@ def test_input_output() -> Generator[tuple[Batch, SavedBatch], None, None]:
7393
),
7494
)
7595

76-
# Load test output.
77-
path = hf_hub_download(
78-
repo_id="microsoft/aurora",
79-
filename="aurora-0.25-small-pretrained-test-output.pickle",
80-
)
81-
with open(path, "rb") as f:
82-
test_output: SavedBatch = pickle.load(f)
83-
8496
yield batch, test_output

0 commit comments

Comments
 (0)