|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from glue.core.component_id import ComponentID |
| 4 | +from glue.core.data import BaseCartesianData |
| 5 | +from glue.utils import compute_statistic |
| 6 | +from glue.core.fixed_resolution_buffer import compute_fixed_resolution_buffer |
| 7 | + |
| 8 | + |
| 9 | +class HiPSData(BaseCartesianData): |
| 10 | + |
| 11 | + def __init__(self, directory_or_url, *, label): |
| 12 | + from reproject.hips import hips_as_dask_array |
| 13 | + self._array, self._wcs = hips_as_dask_array(directory_or_url) |
| 14 | + self._dask_arrays = [] |
| 15 | + # Determine order from array shape |
| 16 | + self._order = int(np.log2(self._array.shape[0] / 5 / self._array.chunksize[0])) |
| 17 | + for level in range(self._order): |
| 18 | + self._dask_arrays.append(hips_as_dask_array(directory_or_url, level=level)[0]) |
| 19 | + self._dask_arrays.append(self._array) |
| 20 | + self.data_cid = ComponentID(label="values", parent=self) |
| 21 | + self._label = label |
| 22 | + super().__init__() |
| 23 | + |
| 24 | + @property |
| 25 | + def label(self): |
| 26 | + return self._label |
| 27 | + |
| 28 | + @property |
| 29 | + def coords(self): |
| 30 | + return self._wcs |
| 31 | + |
| 32 | + @property |
| 33 | + def shape(self): |
| 34 | + return self._array.shape |
| 35 | + |
| 36 | + @property |
| 37 | + def main_components(self): |
| 38 | + return [self.data_cid] |
| 39 | + |
| 40 | + def get_kind(self, cid): |
| 41 | + return "numerical" |
| 42 | + |
| 43 | + def get_data(self, cid, view=None): |
| 44 | + if cid is self.data_cid: |
| 45 | + if view is None: |
| 46 | + raise NotImplementedError("View must be specified for HiPS data") |
| 47 | + if isinstance(view, tuple): |
| 48 | + if len(view) == 2: |
| 49 | + i, j = view |
| 50 | + i = i.ravel() |
| 51 | + j = j.ravel() |
| 52 | + # Only keep non-zero pixels for now |
| 53 | + keep = (i > 0) & (j > 0) |
| 54 | + i = i[keep] |
| 55 | + j = j[keep] |
| 56 | + # Determine minimal separation between pixels. Pick any |
| 57 | + # pixel and use it as a reference pixel, then find the |
| 58 | + # minimum separation from any other pixel to that one. |
| 59 | + iref, jref = i[0], j[0] |
| 60 | + sep = np.hypot(i[1:] - iref, j[1:] - jref) |
| 61 | + min_sep = np.min(sep[sep > 0]) |
| 62 | + # Now that we have min_sep, we can determine which level |
| 63 | + # to use. If the minimum separation is larger than e.g. |
| 64 | + # 2 we can use order - 1, and so on. |
| 65 | + level = max(0, self._order - int(np.log2(min_sep))) |
| 66 | + factor = 2 ** int(self._order - level) |
| 67 | + inew, jnew = view |
| 68 | + inew = inew // factor |
| 69 | + jnew = jnew // factor |
| 70 | + return self._dask_arrays[level].vindex[inew, jnew].compute() |
| 71 | + else: |
| 72 | + raise ValueError("View must be a tuple of two arrays") |
| 73 | + raise NotImplementedError("View must be specified for HiPS data") |
| 74 | + return super().get_data(cid, view=view) |
| 75 | + |
| 76 | + def get_mask(self, subset_state, view=None): |
| 77 | + return subset_state.to_mask(self, view=view) |
| 78 | + |
| 79 | + def compute_fixed_resolution_buffer(self, *args, **kwargs): |
| 80 | + return compute_fixed_resolution_buffer(self, *args, **kwargs) |
| 81 | + |
| 82 | + def compute_statistic( |
| 83 | + self, |
| 84 | + statistic, |
| 85 | + cid, |
| 86 | + axis=None, |
| 87 | + finite=True, |
| 88 | + positive=False, |
| 89 | + subset_state=None, |
| 90 | + percentile=None, |
| 91 | + random_subset=None, |
| 92 | + ): |
| 93 | + data = self._dask_arrays[0].compute() |
| 94 | + return compute_statistic( |
| 95 | + statistic, data, axis=axis, percentile=percentile, finite=finite |
| 96 | + ) |
| 97 | + |
| 98 | + def compute_histogram( |
| 99 | + self, |
| 100 | + cid, |
| 101 | + range=None, |
| 102 | + bins=None, |
| 103 | + log=False, |
| 104 | + subset_state=None, |
| 105 | + subset_group=None, |
| 106 | + ): |
| 107 | + |
| 108 | + raise NotImplementedError("Histogram computation not implemented for HiPS data") |
0 commit comments