@@ -9088,7 +9088,9 @@ def polyfit(
9088
9088
"""
9089
9089
from xarray .core .dataarray import DataArray
9090
9090
9091
- variables = {}
9091
+ # TODO: This can be narrowed to be Variable only if we figure out how to
9092
+ # handle the coordinate values for singular values
9093
+ variables : dict [Hashable , DataArray | Variable ] = {}
9092
9094
skipna_da = skipna
9093
9095
9094
9096
x = np .asarray (_ensure_numeric (self .coords [dim ]).astype (np .float64 ))
@@ -9120,16 +9122,16 @@ def polyfit(
9120
9122
rank = np .linalg .matrix_rank (lhs )
9121
9123
9122
9124
if full :
9123
- rank = DataArray ( rank , name = xname + "matrix_rank" )
9124
- variables [rank . name ] = rank
9125
+ rank = Variable ( dims = (), data = rank )
9126
+ variables [xname + "matrix_rank" ] = rank
9125
9127
_sing = np .linalg .svd (lhs , compute_uv = False )
9128
+ # Using a DataArray here because `degree_dim` coordinate values need not
9126
9129
sing = DataArray (
9127
9130
_sing ,
9128
9131
dims = (degree_dim ,),
9129
9132
coords = {degree_dim : np .arange (rank - 1 , - 1 , - 1 )},
9130
- name = xname + "singular_values" ,
9131
9133
)
9132
- variables [sing . name ] = sing
9134
+ variables [xname + "singular_values" ] = sing
9133
9135
9134
9136
# If we have a coordinate get its underlying dimension.
9135
9137
(true_dim ,) = self .coords [dim ].dims
@@ -9184,29 +9186,32 @@ def polyfit(
9184
9186
# Thus a ReprObject => polyfit was called on a DataArray
9185
9187
name = ""
9186
9188
9187
- coeffs = Variable (data = coeffs / scale_da , dims = (degree_dim ,) + other_dims )
9188
- variables [name + "polyfit_coefficients" ] = coeffs
9189
+ variables [name + "polyfit_coefficients" ] = Variable (
9190
+ data = coeffs / scale_da , dims = (degree_dim ,) + other_dims
9191
+ )
9189
9192
9190
9193
if full or (cov is True ):
9191
- residuals = Variable (
9194
+ variables [ name + "polyfit_residuals" ] = Variable (
9192
9195
data = residuals if var .ndim > 1 else residuals .squeeze (),
9193
9196
dims = other_dims ,
9194
9197
)
9195
- variables [name + "polyfit_residuals" ] = residuals
9196
9198
9197
9199
if cov :
9198
9200
Vbase = np .linalg .inv (np .dot (lhs .T , lhs ))
9199
9201
Vbase /= np .outer (scale , scale )
9202
+ if TYPE_CHECKING :
9203
+ fac : int | DataArray | Variable
9200
9204
if cov == "unscaled" :
9201
9205
fac = 1
9202
9206
else :
9203
9207
if x .shape [0 ] <= order :
9204
9208
raise ValueError (
9205
9209
"The number of data points must exceed order to scale the covariance matrix."
9206
9210
)
9207
- fac = residuals / (x .shape [0 ] - order )
9208
- covariance = DataArray (Vbase , dims = ("cov_i" , "cov_j" )) * fac
9209
- variables [name + "polyfit_covariance" ] = covariance
9211
+ fac = variables [name + "polyfit_residuals" ] / (x .shape [0 ] - order )
9212
+ variables [name + "polyfit_covariance" ] = (
9213
+ Variable (data = Vbase , dims = ("cov_i" , "cov_j" )) * fac
9214
+ )
9210
9215
9211
9216
return type (self )(
9212
9217
data_vars = variables ,
0 commit comments