Skip to content

Commit

Permalink
fix: only sort transform if model is already sorted (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
slevang authored Nov 17, 2023
1 parent 1878406 commit dfe0001
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 4 additions & 3 deletions xeofs/models/eof_rotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,10 @@ def _transform_algorithm(self, data: DataArray) -> DataArray:
projections = xr.dot(projections, RinvT, dims="mode_m")
# Reorder according to variance
# this must be done in one line: i) select modes according to their variance, ii) replace coords with modes from 1 ... n
projections = projections.isel(
mode=self.data["idx_modes_sorted"].values
).assign_coords(mode=projections.mode)
if self.sorted:
projections = projections.isel(
mode=self.data["idx_modes_sorted"].values
).assign_coords(mode=projections.mode)

# Scale scores by "pseudo" norms
projections = projections * pseudo_norms
Expand Down
14 changes: 8 additions & 6 deletions xeofs/models/mca_rotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,10 @@ def transform(self, **kwargs) -> DataArray | List[DataArray]:
projections1 = projections1.rename({"mode": "mode_m"})
projections1 = xr.dot(projections1, RinvT, dims="mode_m")
# Reorder according to variance
projections1 = projections1.isel(
mode=self.data["idx_modes_sorted"].values
).assign_coords(mode=projections1.mode)
if self.sorted:
projections1 = projections1.isel(
mode=self.data["idx_modes_sorted"].values
).assign_coords(mode=projections1.mode)
# Adapt the sign of the scores
projections1 = projections1 * self.data["modes_sign"]

Expand All @@ -389,9 +390,10 @@ def transform(self, **kwargs) -> DataArray | List[DataArray]:
projections2 = projections2.rename({"mode": "mode_m"})
projections2 = xr.dot(projections2, RinvT, dims="mode_m")
# Reorder according to variance
projections2 = projections2.isel(
mode=self.data["idx_modes_sorted"].values
).assign_coords(mode=projections2.mode)
if self.sorted:
projections2 = projections2.isel(
mode=self.data["idx_modes_sorted"].values
).assign_coords(mode=projections2.mode)
# Determine the sign of the scores
projections2 = projections2 * self.data["modes_sign"]

Expand Down

0 comments on commit dfe0001

Please sign in to comment.