Skip to content

'eof' model fit error #249

@gkb999

Description

@gkb999

Describe the bug
A clear and concise description of what the bug is.

Reproducible Minimal Working Example
Provide a concise Python code snippet that demonstrates the issue. To display the code clearly, use GitHub Flavored Markdown for formatting:

My data looks like:

<xarray.Dataset> Size: 5MB
Dimensions:    (time: 420, bnds: 2, plev: 1, lat: 21, lon: 128)
Coordinates:
  * plev       (plev) float64 8B 5e+04
  * lat        (lat) float64 168B -87.86 -85.1 -82.31 ... -37.67 -34.88 -32.09
  * lon        (lon) float64 1kB -177.2 -174.4 -171.6 ... 174.4 177.2 180.0
  * time       (time) object 3kB 2015-01-16 12:00:00 ... 2049-12-16 12:00:00
Dimensions without coordinates: bnds
Data variables:
    time_bnds  (time, bnds) object 7kB ...
    lat_bnds   (lat, bnds) float64 336B -90.0 -86.58 -86.58 ... -33.49 -30.7
    lon_bnds   (lon, bnds) float64 2kB -178.6 -175.8 -175.8 ... 178.6 -178.6
    zg         (time, plev, lat, lon) float32 5MB 5.057e+03 ... 5.836e+03...

Expected behavior
I ran the following expecting spatial patterns and PCs of the variable.

model = xe.single.EOF(use_coslat=True)

Converted lat-lon,

if 'longitude' in H500_f.coords:
    H500_f = H500_f.rename({'lon_bnds': 'lon'})
if 'latitude' in H500_f.coords:
    H500_f = H500_f.rename({'lat_bnds': 'lat'})

Yet, When I fit the model:

model.fit(H500_f, dim="time")

I get the following error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~\anaconda3\envs\xEOF\Lib\site-packages\xarray\core\dataarray.py:892, in DataArray._getitem_coord(self, key)
    891 try:
--> 892     var = self._coords[key]
    893 except KeyError:

KeyError: 'lat'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_10480\2310289397.py in ?()
----> 1 model.fit(H500_f, dim="time")

~\anaconda3\envs\xEOF\Lib\site-packages\xeofs\single\base_model_single_set.py in ?(self, X, dim, weights)
    147 
    148         self.sample_dims = convert_to_dim_type(dim)
    149 
    150         # Preprocess the data & transform to 2D
--> 151         data2D: DataArray = self.preprocessor.fit_transform(
    152             X, self.sample_dims, weights
    153         )
    154 

~\anaconda3\envs\xEOF\Lib\site-packages\sklearn\utils\_set_output.py in ?(self, X, *args, **kwargs)
    317     @wraps(f)
    318     def wrapped(self, X, *args, **kwargs):
--> 319         data_to_wrap = f(self, X, *args, **kwargs)
    320         if isinstance(data_to_wrap, tuple):
    321             # only wrap the first output for cross decomposition
    322             return_tuple = (

~\anaconda3\envs\xEOF\Lib\site-packages\xeofs\preprocessing\preprocessor.py in ?(self, X, sample_dims, weights)
    265         weights: list[Data] | Data | None = None,
    266     ) -> DataArray:
    267         # Take advantage of the fact that `.fit()` already transforms the data
    268         # to avoid duplicate computation
--> 269         self, X = self._fit_algorithm(X, sample_dims, weights)
    270         return X

~\anaconda3\envs\xEOF\Lib\site-packages\xeofs\preprocessing\preprocessor.py in ?(self, X, sample_dims, weights)
    206         weights = process_parameter("weights", weights, None, self.n_data)
    207 
    208         # 1 | Center, scale and weigh the data
    209         scaler_iterkwargs = {"weights": weights}
--> 210         X = self.scaler.fit_transform(
    211             X=X,
    212             sample_dims=sample_dims,
    213             feature_dims=feature_dims,

~\anaconda3\envs\xEOF\Lib\site-packages\xeofs\preprocessing\list_processor.py in ?(self, X, sample_dims, feature_dims, iter_kwargs)
     83         sample_dims: Dims,
     84         feature_dims: DimsList,
     85         iter_kwargs: dict[str, list[Any]] = {},
     86     ) -> list[Data]:
---> 87         return self.fit(X, sample_dims, feature_dims, iter_kwargs).transform(X)  # type: ignore

~\anaconda3\envs\xEOF\Lib\site-packages\xeofs\preprocessing\list_processor.py in ?(self, X, sample_dims, feature_dims, iter_kwargs)
     66             # Add transformer specific keyword arguments
     67             # For iterable kwargs, use the i-th element of the iterable
     68             kwargs = {k: v[i] for k, v in self._iter_kwargs.items()}
     69             proc: T = self.transformer_class(**self.init_kwargs)
---> 70             proc.fit(x, sample_dims, feature_dims[i], **kwargs)
     71             self.transformers.append(proc)
     72         return self

~\anaconda3\envs\xEOF\Lib\site-packages\xeofs\preprocessing\scaler.py in ?(self, X, sample_dims, feature_dims, weights)
    107                 min=np.finfo(np.float32).eps
    108             )
    109 
    110         if params["with_coslat"]:
--> 111             self.coslat_weights_: DataVar = compute_sqrt_cos_lat_weights(
    112                 data=X, feature_dims=self.feature_dims
    113             )
    114 

~\anaconda3\envs\xEOF\Lib\site-packages\xeofs\utils\xarray_utils.py in ?(data, feature_dims)
    127         weights.name = "coslat_weights"
    128         return weights
    129     elif isinstance(data, xr.Dataset):
    130         return xr.Dataset(
--> 131             {
    132                 var: compute_sqrt_cos_lat_weights(da, feature_dims)
    133                 for var, da in data.data_vars.items()
    134             }

~\anaconda3\envs\xEOF\Lib\site-packages\xeofs\utils\xarray_utils.py in ?(.0)
    131 def compute_sqrt_cos_lat_weights(data: DataVar, feature_dims: Dims) -> DataVar:
--> 132     """Compute the square root of cosine of latitude weights.
    133 
    134     Parameters
    135     ----------

~\anaconda3\envs\xEOF\Lib\site-packages\xeofs\utils\xarray_utils.py in ?(data, feature_dims)
    119 
    120     if isinstance(data, xr.DataArray):
    121         lat_dim = extract_latitude_dimension(feature_dims)
    122 
--> 123         latitudes = data.coords[lat_dim]
    124         weights = sqrt_cos_lat_weights(latitudes)
    125         # Features that cannot be associated to a latitude receive a weight of 1
    126         # weights = weights.where(weights.notnull(), 1)

~\anaconda3\envs\xEOF\Lib\site-packages\xarray\core\coordinates.py in ?(self, key)
    927     def __getitem__(self, key: Hashable) -> T_DataArray:
--> 928         return self._data._getitem_coord(key)

~\anaconda3\envs\xEOF\Lib\site-packages\xarray\core\dataarray.py in ?(self, key)
    891         try:
    892             var = self._coords[key]
    893         except KeyError:
    894             dim_sizes = dict(zip(self.dims, self.shape, strict=True))
--> 895             _, key, var = _get_virtual_variable(self._coords, key, dim_sizes)
    896 
    897         return self._replace_maybe_drop_dims(var, name=key)

~\anaconda3\envs\xEOF\Lib\site-packages\xarray\core\dataset.py in ?(variables, key, dim_sizes)
    217         raise KeyError(key)
    218 
    219     split_key = key.split(".", 1)
    220     if len(split_key) != 2:
--> 221         raise KeyError(key)
    222 
    223     ref_name, var_name = split_key
    224     ref_var = variables[ref_name]

KeyError: 'lat'

Desktop (please complete the following information):

  • OS: Windows
  • xeofs version 3.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions