Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
58369d2
o Fix #1220
rajeeja Jun 24, 2025
cc61a12
o Use warning instead of print
rajeeja Jun 26, 2025
e387a1a
Merge branch 'main' into rajeeja/issue_1220
rajeeja Jun 26, 2025
a78c7ec
o Add uxgrid for groupby operation, TODO: add a small test
rajeeja Jun 30, 2025
165d1e5
Merge branch 'main' into rajeeja/issue_1220
philipc2 Jul 1, 2025
cc1de49
o Add test
rajeeja Jul 1, 2025
e0540b9
Merge branch 'main' into rajeeja/issue_1220
philipc2 Jul 8, 2025
703f76c
Merge branch 'main' into rajeeja/issue_1220
rajeeja Jul 8, 2025
3fd196f
o Handle resample - The attribute is properly preserved during resa…
rajeeja Jul 10, 2025
832d13d
o Remove unused variables
rajeeja Jul 10, 2025
39e1b57
UxResampleWrapper is removed, now both groupby and resample look simi…
rajeeja Jul 17, 2025
971fd77
Merge branch 'main' into rajeeja/issue_1220
erogluorhan Jul 18, 2025
9ccee95
o Add groupby_bin
rajeeja Jul 19, 2025
5968ac3
Merge branch 'rajeeja/issue_1220' of github.com:UXARRAY/uxarray into …
rajeeja Jul 19, 2025
5447868
o Remove docstring as we get original ones from xarray
rajeeja Jul 19, 2025
47eca3c
Merge branch 'main' into rajeeja/issue_1220
erogluorhan Jul 21, 2025
df59191
Merge branch 'main' into rajeeja/issue_1220
rajeeja Aug 5, 2025
aacfe4c
Merge branch 'main' into rajeeja/issue_1220
philipc2 Aug 11, 2025
ccdc0c5
o remove hardcoded aggregations, add dir
rajeeja Aug 11, 2025
a1f452e
o Make all methods consistent
rajeeja Aug 11, 2025
3ccd3c0
Merge branch 'main' into rajeeja/issue_1220
rajeeja Aug 13, 2025
4f10531
Update uxarray/core/dataset.py
rajeeja Aug 19, 2025
b324867
Merge branch 'main' into rajeeja/issue_1220
philipc2 Aug 19, 2025
53de766
Fix groupby, groupby_bins, and resample method signatures to match xa…
Aug 20, 2025
17a5e36
Merge branch 'main' into rajeeja/issue_1220
rajeeja Aug 20, 2025
8fc9323
Refactor Core Implementation: Replaced the proxy-based groupby implem…
Aug 20, 2025
31c6102
Merge branch 'main' into rajeeja/issue_1220
philipc2 Aug 20, 2025
975799b
Address PR feedback: Move ACCESSOR_METHODS to accessors.py and clean …
Aug 20, 2025
b5288e1
Move accessor tests to dedicated test_accessors.py module
Aug 20, 2025
eb2dae5
o Precommit
Aug 20, 2025
7bafb6d
o Restore test_dataset.py
Aug 20, 2025
806a539
Update test/test_accessors.py
rajeeja Aug 21, 2025
33933e8
Merge branch 'main' into rajeeja/issue_1220
philipc2 Aug 22, 2025
b790bb3
Merge branch 'main' into rajeeja/issue_1220
erogluorhan Sep 3, 2025
53d59ef
Merge branch 'main' into rajeeja/issue_1220
erogluorhan Sep 4, 2025
eb1f770
Merge branch 'main' into rajeeja/issue_1220
philipc2 Sep 5, 2025
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
180 changes: 180 additions & 0 deletions test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,186 @@ def test_get_dual():
assert isinstance(dual, UxDataset)
assert len(uxds.data_vars) == len(dual.data_vars)


def test_groupby_preserves_uxgrid():
Comment thread
philipc2 marked this conversation as resolved.
Outdated
"""Test that groupby operations preserve the uxgrid attribute."""
# Create a dataset from a file
uxds = ux.open_dataset(mpas_ds_path, mpas_ds_path)
original_grid = uxds.uxgrid

# Create bins from latitude values (extract data explicitly)
lat_bins = (uxds.latCell > 0).astype(int).values

# Add the bins as a coordinate
uxds = uxds.assign_coords({"lat_bins": ("n_face", lat_bins)})

# Test DataArray groupby preserves uxgrid
da_result = uxds.latCell.groupby(uxds.lat_bins).mean()
assert hasattr(da_result, "uxgrid")
assert da_result.uxgrid is not None

# Test Dataset groupby preserves uxgrid
ds_result = uxds.groupby(uxds.lat_bins).mean()
assert hasattr(ds_result, "uxgrid")
assert ds_result.uxgrid is not None
assert ds_result.uxgrid == original_grid

def test_groupby_bins_preserves_uxgrid():
"""Test that groupby_bins operations preserve the uxgrid attribute."""
# Create a dataset from a file
uxds = ux.open_dataset(mpas_ds_path, mpas_ds_path)
original_grid = uxds.uxgrid

# Create bins from latitude values (extract data explicitly)
lat_bins = [-90, -45, 0, 45, 90]

# Test DataArray groupby_bins preserves uxgrid
da_result = uxds.latCell.groupby_bins(uxds.latCell, bins=lat_bins).mean()
assert hasattr(da_result, "uxgrid")
assert da_result.uxgrid is not None

# Test Dataset groupby_bins preserves uxgrid
ds_result = uxds.groupby_bins(uxds.latCell, bins=lat_bins).mean()
assert hasattr(ds_result, "uxgrid")
assert ds_result.uxgrid is not None
assert ds_result.uxgrid == original_grid



def test_resample_preserves_uxgrid_and_reduces_time():
"""Test that resample operations preserve uxgrid and reduce time dimension."""
import numpy as np
import pandas as pd
import pytest
import xarray as xr
Comment thread
philipc2 marked this conversation as resolved.
Outdated

# Create a simple test with only time dimension
times = pd.date_range("2000-01-01", periods=12, freq="D")
temp_data = np.random.rand(12)

# Create a simple xarray Dataset
xr_ds = xr.Dataset(
{"temperature": ("time", temp_data)},
coords={"time": times}
)

# Open the minimal dataset with a real grid
try:
Comment thread
philipc2 marked this conversation as resolved.
Outdated
# Use existing test file we know works
uxgrid = ux.open_grid(gridfile_ne30)

# Create a UxDataset with this grid
uxds = ux.UxDataset(xr_ds, uxgrid=uxgrid)

print(f"Original dataset dims: {uxds.dims}")
print(f"Original dataset shape: {uxds.temperature.shape}")

# Test the resample method directly
print("Attempting resample...")
result = uxds.temperature.resample(time="1W").mean()

print(f"Resampled result dims: {result.dims}")
print(f"Resampled result shape: {result.shape}")

# Test assertions
assert hasattr(result, "uxgrid"), "uxgrid not preserved on resample"
assert result.uxgrid == uxds.uxgrid, "uxgrid not equal after resample"
assert len(result.time) < len(uxds.time), "time dimension not reduced"

except Exception as e:
import traceback
traceback.print_exc()
pytest.fail(f"Error in resample test: {e}")

def test_resample_preserves_uxgrid():
"""Test that resample preserves the uxgrid attribute."""
import numpy as np
import pandas as pd
import pytest
Comment thread
philipc2 marked this conversation as resolved.
Outdated

# Create a simple dataset with a time dimension
times = pd.date_range("2000-01-01", periods=12, freq="D")
data = np.random.rand(12)

# Create a simple xarray Dataset
ds = xr.Dataset(
{"temperature": ("time", data)},
coords={"time": times}
)

# Create a UxDataset with a real grid
uxds = ux.open_dataset(gridfile_ne30, gridfile_ne30)
original_uxgrid = uxds.uxgrid

# Create a new UxDataset with our time data and the real grid
uxds_time = ux.UxDataset(ds, uxgrid=original_uxgrid)

# Test DataArray resample preserves uxgrid
da_result = uxds_time.temperature.resample(time="1W").mean()
assert hasattr(da_result, "uxgrid"), "uxgrid not preserved on DataArray resample"
assert da_result.uxgrid is original_uxgrid, "uxgrid not identical after DataArray resample"

# Test Dataset resample preserves uxgrid
ds_result = uxds_time.resample(time="1W").mean()
assert hasattr(ds_result, "uxgrid"), "uxgrid not preserved on Dataset resample"
assert ds_result.uxgrid is original_uxgrid, "uxgrid not identical after Dataset resample"


def test_resample_reduces_time_dimension():
"""Test that resample properly reduces the time dimension."""
import numpy as np
import pandas as pd
import pytest
Comment thread
philipc2 marked this conversation as resolved.
Outdated

# Create dataset with daily data for a year
times = pd.date_range("2000-01-01", periods=365, freq="D")
data = np.random.rand(365)

# Create a simple xarray Dataset
ds = xr.Dataset(
{"temperature": ("time", data)},
coords={"time": times}
)

# Create a UxDataset
uxds = ux.UxDataset(ds, uxgrid=ux.open_grid(gridfile_ne30))

# Test monthly resampling reduces from 365 days to 12 months
monthly = uxds.resample(time="1M").mean()
assert "time" in monthly.dims, "time dimension missing after resample"
assert monthly.dims["time"] < uxds.dims["time"], "time dimension not reduced"
assert monthly.dims["time"] <= 12, "monthly resampling should give 12 or fewer time points"


def test_resample_with_cftime():
"""Test that resample works with cftime objects."""
import numpy as np
import pytest

try:
import cftime
except ImportError:
pytest.skip("cftime package not available")

# Create a dataset with cftime DatetimeNoLeap objects
times = [cftime.DatetimeNoLeap(2000, month, 15) for month in range(1, 13)]
data = np.random.rand(12)

# Create a simple xarray Dataset with cftime
ds = xr.Dataset(
{"temperature": ("time", data)},
coords={"time": times}
)

# Create a UxDataset
uxds = ux.UxDataset(ds, uxgrid=ux.open_grid(gridfile_ne30))

# Test that quarterly resampling works with cftime
quarterly = uxds.resample(time="Q").mean()
assert hasattr(quarterly, "uxgrid"), "uxgrid not preserved with cftime resampling"
assert "time" in quarterly.dims, "time dimension missing after cftime resample"
assert quarterly.dims["time"] < uxds.dims["time"], "time dimension not reduced with cftime"

# Uncomment the following test if you want to include it, ensuring you handle potential failures.
# def test_read_from_https():
# """Tests reading a dataset from a HTTPS link."""
Expand Down
Loading
Loading