Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom tokenization function for NetCDFDataProxy objects #6231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions benchmarks/benchmarks/load/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,58 @@ def time_many_var_load(self) -> None:
_ = load(str(self.FILE_PATH))


class ManyCubes:
FILE_PATH = BENCHMARK_DATA / "many_cube_file.nc"

@staticmethod
def _create_file(save_path: str) -> None:
"""Run externally - everything must be self-contained."""
import numpy as np

from iris import save
from iris.coords import AuxCoord, DimCoord
from iris.cube import Cube, CubeList

data_len = 81920
bnds_len = 3
data = np.arange(data_len).astype(np.float32)
bnds_data = (
np.arange(data_len * bnds_len)
.astype(np.float32)
.reshape(data_len, bnds_len)
)
time = DimCoord(np.array([0]), standard_name="time")
lat = AuxCoord(
data, bounds=bnds_data, standard_name="latitude", units="degrees"
)
lon = AuxCoord(
data, bounds=bnds_data, standard_name="longitude", units="degrees"
)
cube = Cube(data.reshape(1, -1), units="unknown")
cube.add_dim_coord(time, 0)
cube.add_aux_coord(lat, 1)
cube.add_aux_coord(lon, 1)

n_cubes = 100
cubes = CubeList()
for i in range(n_cubes):
cube = cube.copy()
cube.long_name = f"var_{i}"
cubes.append(cube)
save(cubes, save_path)

def setup_cache(self) -> None:
if not REUSE_DATA or not self.FILE_PATH.is_file():
# See :mod:`benchmarks.generate_data` docstring for full explanation.
_ = run_function_elsewhere(
self._create_file,
str(self.FILE_PATH),
)

def time_many_cube_load(self) -> None:
_ = load(str(self.FILE_PATH))


class StructuredFF:
"""Test structured loading of a large-ish fieldsfile.

Expand Down
6 changes: 6 additions & 0 deletions lib/iris/fileformats/netcdf/_thread_safe_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ def ndim(self):
def dask_meta(self):
return np.ma.array(np.empty((0,) * self.ndim, dtype=self.dtype), mask=True)

def __dask_tokenize__(self):
# Dask uses this function to uniquely identify the "array".
# A custom function is slightly faster than general object tokenization,
# which improves the speed of loading small NetCDF files.
return f"<{self.__class__.__name__} path={self.path!r} variable_name={self.variable_name!r}>"

def __getitem__(self, keys):
# Using a DatasetWrapper causes problems with invalid ID's and the
# netCDF4 library, presumably because __getitem__ gets called so many
Expand Down
Loading