Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ dependencies = [
"bioio-tifffile",
"centrosome",
"cp-measure",
"crick",
"dask-image",
"dask",
"decorator",
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ bioio-base==1.0.7
bioio==1.6.1
centrosome==1.3.3
cp-measure==0.1.13
crick==0.0.8
cython==3.2.4
dask-image==2025.11.0
dask==2025.11.0
Expand Down
14 changes: 1 addition & 13 deletions scallops/tests/test_xr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import numpy as np
import pytest
import xarray as xr

from scallops.xr import apply_data_array, dask_grouped_quantiles
from scallops.xr import apply_data_array


@pytest.fixture
Expand All @@ -12,17 +11,6 @@ def image(array_A1_102_aln):
) # ops swaps z and t in saved tif


@pytest.mark.io
def test_dask_grouped_quantiles(image):
dask_image = image.squeeze() # ops swaps z and t in saved tif
dask_image = dask_image.chunk(dict(t=1, y=256, x=256))
q = [0.5, 0.75]
dask_results = dask_grouped_quantiles(dask_image, dims=["t", "c"], q=q).compute()
results = image.squeeze().quantile(dim=["y", "x"], q=q)
assert np.abs(dask_results.isel(quantile=0) - results.isel(quantile=0)).max() < 0.8
assert np.abs(dask_results.isel(quantile=1) - results.isel(quantile=1)).max() < 1.75


@pytest.mark.io
def test_data_array(image):
def add_data_array(x: xr.DataArray, y: float):
Expand Down
70 changes: 0 additions & 70 deletions scallops/xr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
- The SCALLOPS development team
"""

import importlib
import itertools
from collections.abc import Callable, Sequence
from typing import Any, Literal, Union

import dask
import dask.array as da
import numpy as np
import xarray as xr

Expand Down Expand Up @@ -92,73 +89,6 @@ def _get_dims(
return _dims


def dask_grouped_quantiles(
array: xr.DataArray, dims: list[str], q: list[float]
) -> xr.DataArray:
"""Compute quantiles for grouped data using Dask.

This function calculates the specified quantiles for the given dimensions in a
Dask-backed Xarray DataArray. It uses Dask's percentile computation to handle
large datasets efficiently.

:param array: The input DataArray containing the data.
:param dims: List of dimensions over which to compute the quantiles.
:param q: List of quantiles to compute, each value should be between 0 and 1.
:return: A DataArray containing the computed quantiles for the specified dimensions.

:raises AssertionError:
If no quantiles are provided in the `q` list.
:raises ValueError:
If a specified dimension is not found in the DataArray and `missing_dims`
is set to "error".

:example:

.. code-block:: python

import xarray as xr
import numpy as np
import dask.array as da

data = da.random.random((10, 20, 30), chunks=(5, 10, 15))
array = xr.DataArray(data, dims=["x", "y", "z"])

# Compute quantiles for dimensions 'x' and 'y'
quantiles = dask_grouped_quantiles(array, ["x", "y"], [0.25, 0.5, 0.75])
print(quantiles)
"""
assert len(q) > 0, "No quantiles provided"
dims = _get_dims(array, dims)

coords = {d: array.coords[d] for d in dims}
coords["quantile"] = q

quantiles = [_q * 100 for _q in q]
results = xr.DataArray(
da.zeros((len(q),) + tuple([array.sizes[d] for d in dims])),
dims=["quantile"] + dims,
coords=coords,
)

dim_vals = [array[d].values for d in dims]
internal_method = "tdigest"

try:
importlib.import_module("crick")
except ModuleNotFoundError:
internal_method = "default"
with dask.config.set(**{"array.slicing.split_large_chunks": True}):
for dim_val in itertools.product(*dim_vals):
sel = dict(zip(dims, dim_val))
values = da.percentile(
array.sel(sel).data.reshape(-1),
quantiles,
internal_method=internal_method,
)
results.loc[sel] = values
return results


def apply_data_array(
array: xr.DataArray,
dims: list[str],
Expand Down