Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions perf_tests/compute_air.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import xarray_sql as xql

if __name__ == "__main__":
air = xr.tutorial.open_dataset("air_temperature")
chunks = {"time": 240}
air = air.chunk(chunks)
air = xr.tutorial.open_dataset("air_temperature")
chunks = {"time": 240}
air = air.chunk(chunks)

df = xql.read_xarray(air).read_pandas()
df = xql.read_xarray(air).read_pandas()

print(len(df))
print(len(df))
34 changes: 17 additions & 17 deletions perf_tests/groupby_air.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@


if __name__ == "__main__":
air = xr.tutorial.open_dataset("air_temperature")
chunks = {"time": 240, "lat": 5, "lon": 7}
air = air.chunk(chunks)
air_small = air.isel(
time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10)
).chunk(chunks)
air = xr.tutorial.open_dataset("air_temperature")
chunks = {"time": 240, "lat": 5, "lon": 7}
air = air.chunk(chunks)
air_small = air.isel(
time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10)
).chunk(chunks)

df = xql.read_xarray_table(air_small)
df = xql.read_xarray_table(air_small)

ctx = SessionContext()
ctx.register_table("air", df)
ctx = SessionContext()
ctx.register_table("air", df)

query = ctx.sql(
"""
query = ctx.sql(
"""
SELECT
"lat", "lon", SUM("air") as air_total
FROM
"air"
GROUP BY
"lat", "lon"
"""
)
)

result = query.collect()
result = query.collect()

expected = air_small.sizes["lat"] * air_small.sizes["lon"]
actual = sum(len(batch) for batch in result)
assert actual == expected, f"Length must be {expected}, but was {actual}."
print(expected)
expected = air_small.sizes["lat"] * air_small.sizes["lon"]
actual = sum(len(batch) for batch in result)
assert actual == expected, f"Length must be {expected}, but was {actual}."
print(expected)
28 changes: 14 additions & 14 deletions perf_tests/groupby_air_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
from datafusion import SessionContext

if __name__ == "__main__":
air = xr.tutorial.open_dataset("air_temperature")
chunks = {"time": 240}
air = air.chunk(chunks)
air = xr.tutorial.open_dataset("air_temperature")
chunks = {"time": 240}
air = air.chunk(chunks)

df = xql.read_xarray_table(air)
df = xql.read_xarray_table(air)

ctx = SessionContext()
ctx.register_table("air", df)
ctx = SessionContext()
ctx.register_table("air", df)

query = ctx.sql(
"""
query = ctx.sql(
"""
SELECT
"lat", "lon", SUM("air") as air_total
FROM
"air"
GROUP BY
"lat", "lon"
"""
)
)

result = query.collect()
result = query.collect()

expected = air.sizes["lat"] * air.sizes["lon"]
actual = sum(len(batch) for batch in result)
expected = air.sizes["lat"] * air.sizes["lon"]
actual = sum(len(batch) for batch in result)

assert actual == expected, f"Length must be {expected}, but was {actual}."
print(expected)
assert actual == expected, f"Length must be {expected}, but was {actual}."
print(expected)
14 changes: 7 additions & 7 deletions perf_tests/sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import xarray_sql as xql

if __name__ == "__main__":
air = xr.tutorial.open_dataset("air_temperature")
chunks = {"time": 240, "lat": 5, "lon": 7}
air = xr.tutorial.open_dataset("air_temperature")
chunks = {"time": 240, "lat": 5, "lon": 7}

air_small = air.isel(
time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10)
).chunk(chunks)
air_small = air.isel(
time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10)
).chunk(chunks)

df = xql.read_xarray(air_small).read_pandas()
df = xql.read_xarray(air_small).read_pandas()

print(len(df))
print(len(df))
12 changes: 7 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ module-name = "xarray_sql._native"
[tool.setuptools.packages.find]
exclude = ["demo", "perf_tests", "tests", "tests.*"]

[tool.pyink]
[tool.ruff]
line-length = 80
preview = true
pyink-indentation = 2
pyink-use-majority-quotes = true
indent-width = 4

[tool.ruff.format]
indent-style = "space"
quote-style = "double"

[tool.mypy]
python_version = "3.11"
Expand Down Expand Up @@ -98,7 +100,7 @@ dev = [
"xarray_sql[test]",
"xarray_sql[docs]",
"py-spy>=0.4.0",
"pyink>=24.10.1",
"ruff>=0.15.10",
"maturin>=1.9.1",
]

Expand Down
178 changes: 92 additions & 86 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,139 +6,145 @@


def rand_wx(start: str, end: str) -> xr.Dataset:
np.random.seed(42)
lat = np.linspace(-90, 90, num=720)
lon = np.linspace(-180, 180, num=1440)
time = pd.date_range(start, end, freq="h")
level = np.array([1000, 500], dtype=np.int32)
reference_time = pd.Timestamp(start)
temperature = 15 + 8 * np.random.randn(720, 1440, len(time), len(level))
precipitation = 10 * np.random.rand(720, 1440, len(time), len(level))
return xr.Dataset(
data_vars=dict(
temperature=(["lat", "lon", "time", "level"], temperature),
precipitation=(["lat", "lon", "time", "level"], precipitation),
),
coords=dict(
lat=lat,
lon=lon,
time=time,
level=level,
reference_time=reference_time,
),
attrs=dict(description="Random weather."),
)
np.random.seed(42)
lat = np.linspace(-90, 90, num=720)
lon = np.linspace(-180, 180, num=1440)
time = pd.date_range(start, end, freq="h")
level = np.array([1000, 500], dtype=np.int32)
reference_time = pd.Timestamp(start)
temperature = 15 + 8 * np.random.randn(720, 1440, len(time), len(level))
precipitation = 10 * np.random.rand(720, 1440, len(time), len(level))
return xr.Dataset(
data_vars=dict(
temperature=(["lat", "lon", "time", "level"], temperature),
precipitation=(["lat", "lon", "time", "level"], precipitation),
),
coords=dict(
lat=lat,
lon=lon,
time=time,
level=level,
reference_time=reference_time,
),
attrs=dict(description="Random weather."),
)


def create_large_dataset(time_steps=1000, lat_points=100, lon_points=100):
"""Create a large xarray dataset for memory testing."""
np.random.seed(42)
"""Create a large xarray dataset for memory testing."""
np.random.seed(42)

time = pd.date_range("2020-01-01", periods=time_steps, freq="h")
lat = np.linspace(-90, 90, lat_points)
lon = np.linspace(-180, 180, lon_points)
time = pd.date_range("2020-01-01", periods=time_steps, freq="h")
lat = np.linspace(-90, 90, lat_points)
lon = np.linspace(-180, 180, lon_points)

temp_data = np.random.rand(time_steps, lat_points, lon_points) * 40 - 10
precip_data = np.random.rand(time_steps, lat_points, lon_points) * 100
temp_data = np.random.rand(time_steps, lat_points, lon_points) * 40 - 10
precip_data = np.random.rand(time_steps, lat_points, lon_points) * 100

return xr.Dataset(
{
"temperature": (["time", "lat", "lon"], temp_data),
"precipitation": (["time", "lat", "lon"], precip_data),
},
coords={"time": time, "lat": lat, "lon": lon},
)
return xr.Dataset(
{
"temperature": (["time", "lat", "lon"], temp_data),
"precipitation": (["time", "lat", "lon"], precip_data),
},
coords={"time": time, "lat": lat, "lon": lon},
)


@pytest.fixture
def air():
ds = xr.tutorial.open_dataset("air_temperature")
chunks = {"time": 240}
return ds.chunk(chunks)
ds = xr.tutorial.open_dataset("air_temperature")
chunks = {"time": 240}
return ds.chunk(chunks)


@pytest.fixture
def air_small(air):
return air.isel(time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10)).chunk(
{"time": 240}
)
return air.isel(
time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10)
).chunk({"time": 240})


@pytest.fixture
def randwx():
return rand_wx("1995-01-13T00", "1995-01-13T01")
return rand_wx("1995-01-13T00", "1995-01-13T01")


@pytest.fixture
def large_ds():
return create_large_dataset().chunk({"time": 25})
return create_large_dataset().chunk({"time": 25})


@pytest.fixture
def air_dataset_small():
ds = xr.tutorial.open_dataset("air_temperature").chunk({"time": 240})
return ds.isel(time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10))
ds = xr.tutorial.open_dataset("air_temperature").chunk({"time": 240})
return ds.isel(time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10))


@pytest.fixture
def air_dataset_large():
return xr.tutorial.open_dataset("air_temperature").chunk({"time": 240})
return xr.tutorial.open_dataset("air_temperature").chunk({"time": 240})


@pytest.fixture
def rasm_ds():
"""rasm uses cftime.DatetimeNoLeap (noleap / 365_day) for time."""
return xr.tutorial.open_dataset("rasm")
"""rasm uses cftime.DatetimeNoLeap (noleap / 365_day) for time."""
return xr.tutorial.open_dataset("rasm")


@pytest.fixture
def weather_dataset():
ds = rand_wx("2023-01-01T00", "2023-01-01T12")
return ds.isel(time=slice(0, 6), lat=slice(0, 10), lon=slice(0, 10)).chunk(
{"time": 3}
)
ds = rand_wx("2023-01-01T00", "2023-01-01T12")
return ds.isel(time=slice(0, 6), lat=slice(0, 10), lon=slice(0, 10)).chunk(
{"time": 3}
)


@pytest.fixture
def synthetic_dataset():
return create_large_dataset(
time_steps=50, lat_points=20, lon_points=20
).chunk({"time": 25})
return create_large_dataset(
time_steps=50, lat_points=20, lon_points=20
).chunk({"time": 25})


@pytest.fixture
def station_dataset():
return xr.Dataset(
{
"station_id": (["station"], [1, 2, 3, 4, 5]),
"elevation": (["station"], [100, 250, 500, 750, 1000]),
"name": (
["station"],
["Station_A", "Station_B", "Station_C", "Station_D", "Station_E"],
),
}
).chunk({"station": 5})
return xr.Dataset(
{
"station_id": (["station"], [1, 2, 3, 4, 5]),
"elevation": (["station"], [100, 250, 500, 750, 1000]),
"name": (
["station"],
[
"Station_A",
"Station_B",
"Station_C",
"Station_D",
"Station_E",
],
),
}
).chunk({"station": 5})


@pytest.fixture
def air_and_stations():
air = (
xr.tutorial.open_dataset("air_temperature")
.isel(time=slice(0, 12), lat=slice(0, 5), lon=slice(0, 8))
.chunk({"time": 6})
)
stations = xr.Dataset(
{
"station_id": (["station"], [101, 102, 103]),
"lat": (
["station"],
[air.lat.values[0], air.lat.values[2], air.lat.values[4]],
),
"lon": (
["station"],
[air.lon.values[1], air.lon.values[3], air.lon.values[5]],
),
"elevation": (["station"], [100, 250, 500]),
}
).chunk({"station": 3})
return air, stations
air = (
xr.tutorial.open_dataset("air_temperature")
.isel(time=slice(0, 12), lat=slice(0, 5), lon=slice(0, 8))
.chunk({"time": 6})
)
stations = xr.Dataset(
{
"station_id": (["station"], [101, 102, 103]),
"lat": (
["station"],
[air.lat.values[0], air.lat.values[2], air.lat.values[4]],
),
"lon": (
["station"],
[air.lon.values[1], air.lon.values[3], air.lon.values[5]],
),
"elevation": (["station"], [100, 250, 500]),
}
).chunk({"station": 3})
return air, stations
Loading
Loading