Skip to content

Commit

Permalink
Merge pull request #37 from jlvdb/numpy_v2
Browse files Browse the repository at this point in the history
Numpy 2.0 support
  • Loading branch information
jlvdb authored Jun 17, 2024
2 parents c384104 + a1fb8e7 commit 97b8e18
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 112 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change log
==========

Version 2.5.8
-------------

Updates to be compatible with numpy version 2.0.


Version 2.5.7
-------------
Expand Down
3 changes: 1 addition & 2 deletions src/yaw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
(Ruhr-Universität Bochum, Astronomisches Institut)
"""


import logging as _logging

_logging.getLogger(__name__).addHandler(_logging.NullHandler()) # noqa
Expand Down Expand Up @@ -55,4 +54,4 @@
"HistogramData",
"PairCountResult",
]
__version__ = "2.5.7"
__version__ = "2.5.8"
12 changes: 6 additions & 6 deletions src/yaw/catalogs/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,31 +219,31 @@ def pos(self) -> CoordSky:

@property
@abstractmethod
def ra(self) -> NDArray[np.float_]:
def ra(self) -> NDArray[np.float64]:
"""Get an array of the right ascension values in radians."""
pass

@property
@abstractmethod
def dec(self) -> NDArray[np.float_]:
def dec(self) -> NDArray[np.float64]:
"""Get an array of the declination values in radians."""
pass

@property
@abstractmethod
def redshifts(self) -> NDArray[np.float_] | None:
def redshifts(self) -> NDArray[np.float64] | None:
"""Get the redshifts as array or ``None`` if not available."""
pass

@property
@abstractmethod
def weights(self) -> NDArray[np.float_]:
def weights(self) -> NDArray[np.float64]:
"""Get the object weights as array or ``None`` if not available."""
pass

@property
@abstractmethod
def patch(self) -> NDArray[np.int_]:
def patch(self) -> NDArray[np.int64]:
"""Get the patch indices of each object as array."""
pass

Expand All @@ -264,7 +264,7 @@ def total(self) -> float:
available."""

@abstractmethod
def get_totals(self) -> NDArray[np.float_]:
def get_totals(self) -> NDArray[np.float64]:
"""Get an array of the sum of weights or number of objects in each
patch."""

Expand Down
18 changes: 9 additions & 9 deletions src/yaw/catalogs/scipy/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def _worker_correlate(


def _worker_true_redshifts(
args: tuple[PatchCatalog, NDArray[np.float_]]
) -> NDArray[np.float_]:
args: tuple[PatchCatalog, NDArray[np.float64]]
) -> NDArray[np.float64]:
return utils.count_histogram_patch(*args)


Expand Down Expand Up @@ -136,7 +136,7 @@ def __init__(
with TimedLog(self._logger.info, f"processed {n_obj_str} records"):
limits = LimitTracker()
patches: dict[int, PatchCatalog] = {}
patch_iter = data.groupby(patch_name)
patch_iter = data.groupby(patch_name, observed=True)
if progress:
patch_iter = job_progress_bar(patch_iter, total=n_patches)
for patch_id, patch_data in patch_iter:
Expand Down Expand Up @@ -260,22 +260,22 @@ def has_weights(self) -> bool:
return all(patch.has_weights() for patch in self._patches.values())

@property
def ra(self) -> NDArray[np.float_]:
def ra(self) -> NDArray[np.float64]:
return np.concatenate([patch.ra for patch in iter(self)])

@property
def dec(self) -> NDArray[np.float_]:
def dec(self) -> NDArray[np.float64]:
return np.concatenate([patch.dec for patch in iter(self)])

@property
def redshifts(self) -> NDArray[np.float_] | None:
def redshifts(self) -> NDArray[np.float64] | None:
if self.has_redshifts():
return np.concatenate([patch.redshifts for patch in iter(self)])
else:
return None

@property
def weights(self) -> NDArray[np.float_]:
def weights(self) -> NDArray[np.float64]:
weights = []
for patch in iter(self):
if patch.has_weights():
Expand All @@ -285,7 +285,7 @@ def weights(self) -> NDArray[np.float_]:
return np.concatenate(weights)

@property
def patch(self) -> NDArray[np.int_]:
def patch(self) -> NDArray[np.int64]:
return np.concatenate([np.full(len(patch), patch.id) for patch in iter(self)])

def get_min_redshift(self) -> float:
Expand All @@ -298,7 +298,7 @@ def get_max_redshift(self) -> float:
def total(self) -> float:
return self.get_totals().sum()

def get_totals(self) -> NDArray[np.float_]:
def get_totals(self) -> NDArray[np.float64]:
return np.array([patch.total for patch in self._patches.values()])

@property
Expand Down
4 changes: 2 additions & 2 deletions src/yaw/catalogs/scipy/kdtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SphericalKDTree:
def __init__(
self,
position: Coordinate,
weights: NDArray[np.float_] | None = None,
weights: NDArray[np.float64] | None = None,
leafsize: int = 16,
) -> None:
"""Build a new tree from a set of coordinates.
Expand Down Expand Up @@ -67,7 +67,7 @@ def total(self) -> float:
def count(
self,
other: SphericalKDTree,
scales: NDArray[np.float_],
scales: NDArray[np.float64],
dist_weight_scale: float | None = None,
weight_res: int = 50,
) -> NDArray:
Expand Down
22 changes: 12 additions & 10 deletions src/yaw/catalogs/scipy/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,15 @@ def data(self) -> DataFrame:
return self._data

@property
def ra(self) -> NDArray[np.float_]:
def ra(self) -> NDArray[np.float64]:
"""Get an array of the right ascension values in radians.
Raises a :obj:`CachingError` if data is not loaded."""
self.require_loaded()
return self._data["ra"].to_numpy()

@property
def dec(self) -> NDArray[np.float_]:
def dec(self) -> NDArray[np.float64]:
"""Get an array of the declination values in radians.
Raises a :obj:`CachingError` if data is not loaded."""
Expand All @@ -260,7 +260,7 @@ def pos(self) -> CoordSky:
return CoordSky(self.ra, self.dec)

@property
def redshifts(self) -> NDArray[np.float_]:
def redshifts(self) -> NDArray[np.float64]:
"""Get the redshifts as array or ``None`` if not available.
Raises a :obj:`CachingError` if data is not loaded."""
Expand All @@ -271,7 +271,7 @@ def redshifts(self) -> NDArray[np.float_]:
return None

@property
def weights(self) -> NDArray[np.float_]:
def weights(self) -> NDArray[np.float64]:
"""Get the object weights as array or ``None`` if not available.
Raises a :obj:`CachingError` if data is not loaded."""
Expand Down Expand Up @@ -312,7 +312,7 @@ def radius(self) -> DistSky:
return self._radius

def iter_bins(
self, z_bins: NDArray[np.float_], allow_no_redshift: bool = False
self, z_bins: NDArray[np.float64], allow_no_redshift: bool = False
) -> Iterator[tuple[Interval, PatchCatalog]]:
"""Iterate the patch in bins of redshift.
Expand All @@ -335,7 +335,9 @@ def iter_bins(
for intv in pd.IntervalIndex.from_breaks(z_bins, closed="left"):
yield intv, self
else:
for intv, bin_data in self._data.groupby(pd.cut(self.redshifts, z_bins)):
for intv, bin_data in self._data.groupby(
pd.cut(self.redshifts, z_bins), observed=True
):
yield intv, PatchCatalog(
self.id,
bin_data,
Expand All @@ -356,7 +358,7 @@ def get_tree(self, **kwargs) -> SphericalKDTree:
# the scipy.cluster module.


def assign_patches(centers: Coordinate, position: Coordinate) -> NDArray[np.int_]:
def assign_patches(centers: Coordinate, position: Coordinate) -> NDArray[np.int64]:
"""Assign objects based on their coordinate to a list of points based on
proximit."""
patches, dist = vq.vq(position.to_3d().values, centers.to_3d().values)
Expand All @@ -368,7 +370,7 @@ def assign_patches(centers: Coordinate, position: Coordinate) -> NDArray[np.int_

def treecorr_patches(
position: Coordinate, n_patches: int, **kwargs
) -> tuple[Coord3D, NDArray[np.int_]]:
) -> tuple[Coord3D, NDArray[np.int64]]:
"""Use the *k*-means clustering algorithm of :obj:`treecorr.Catalog` to
generate spatial patches and assigning objects to those patches.
"""
Expand All @@ -383,7 +385,7 @@ def treecorr_patches(
xyz = np.atleast_2d(cat.patch_centers)
centers = Coord3D.from_array(xyz)
if n_patches == 1:
patches = np.zeros(len(position), dtype=np.int_)
patches = np.zeros(len(position), dtype=np.int64)
else:
patches = assign_patches(centers=centers, position=position)
del cat # might not be necessary
Expand All @@ -395,7 +397,7 @@ def treecorr_patches(

def scipy_patches(
position: Coordinate, n_patches: int, n_max: int = 500_000
) -> tuple[Coord3D, NDArray[np.int_]]:
) -> tuple[Coord3D, NDArray[np.int64]]:
"""Use the *k*-means clustering algorithm of :obj:`scipy.cluster` to
generate spatial patches and assigning objects to those patches.
"""
Expand Down
16 changes: 8 additions & 8 deletions src/yaw/catalogs/scipy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,18 @@ def merge_pairs_patches(


def count_histogram_patch(
patch: PatchCatalog, z_bins: NDArray[np.float_]
) -> NDArray[np.float_]:
patch: PatchCatalog, z_bins: NDArray[np.float64]
) -> NDArray[np.float64]:
"""Compute a histogram of redshifts in a single patch.
Args:
patch (:obj:`yaw.catalogs.scipy.PatchCatalog`):
The input patch catalogue.
z_bins (:obj:`NDArray[np.float_]`):
z_bins (:obj:`NDArray[np.float64]`):
The bin edges including the right-most edge.
Returns:
:obj:`NDArray[np.float_]`:
:obj:`NDArray[np.float64]`:
Counts in the provided redshift bins.
"""
is_loaded = patch.is_loaded()
Expand All @@ -211,17 +211,17 @@ def count_histogram_patch(


def merge_histogram_patches(
hist_counts: NDArray[np.float_],
z_bins: NDArray[np.float_],
hist_counts: NDArray[np.float64],
z_bins: NDArray[np.float64],
sampling_config: ResamplingConfig | None = None,
) -> HistData:
"""Merge redshift histogram from patches into a histogram data container.
Args:
hist_counts (:obj:`NDArray[np.float_]`):
hist_counts (:obj:`NDArray[np.float64]`):
A two-dimensional array with histogram counts with shape
`(n_patches, n_bins)`.
z_bins (:obj:`NDArray[np.float_]`):
z_bins (:obj:`NDArray[np.float64]`):
The bin edges including the right-most edge.
sampling_config: (:obj:`yaw.config.ResamplingConfig`, optional):
Specify the resampling method and its configuration.
Expand Down
18 changes: 9 additions & 9 deletions src/yaw/catalogs/treecorr/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _iter_bin_masks(


def take_subset(
cat: TreecorrCatalog, items: NDArray[np.bool_] | NDArray[np.int_] | slice
cat: TreecorrCatalog, items: NDArray[np.bool_] | NDArray[np.int64] | slice
) -> TreecorrCatalog | EmptyCatalog:
"""Construct a new TreecorrCatalog with a subset of its entries."""
ra = cat.ra[items]
Expand Down Expand Up @@ -85,7 +85,7 @@ def __len__(self) -> int:
def total(self) -> float:
return 0.0

def get_totals(self) -> NDArray[np.float_]:
def get_totals(self) -> NDArray[np.float64]:
return np.zeros(self.n_patches)


Expand Down Expand Up @@ -234,23 +234,23 @@ def has_weights(self) -> bool:
return self.weights is not None

@property
def ra(self) -> NDArray[np.float_]:
def ra(self) -> NDArray[np.float64]:
return self._catalog.ra

@property
def dec(self) -> NDArray[np.float_]:
def dec(self) -> NDArray[np.float64]:
return self._catalog.dec

@property
def redshifts(self) -> NDArray[np.float_] | None:
def redshifts(self) -> NDArray[np.float64] | None:
return self._catalog.r

@property
def weights(self) -> NDArray[np.float_]:
def weights(self) -> NDArray[np.float64]:
return self._catalog.w

@property
def patch(self) -> NDArray[np.int_]:
def patch(self) -> NDArray[np.int64]:
return self._catalog.patch

def get_min_redshift(self) -> float:
Expand All @@ -273,7 +273,7 @@ def get_max_redshift(self) -> float:
def total(self) -> float:
return self._catalog.sumw

def get_totals(self) -> NDArray[np.float_]:
def get_totals(self) -> NDArray[np.float64]:
return np.array([patch.sumw for patch in iter(self)])

@property
Expand All @@ -296,7 +296,7 @@ def radii(self) -> DistSky:
return DistSky.from_dists(radii)

def iter_bins(
self, z_bins: NDArray[np.float_], allow_no_redshift: bool = False
self, z_bins: NDArray[np.float64], allow_no_redshift: bool = False
) -> Iterator[tuple[Interval, TreecorrCatalog | EmptyCatalog]]:
"""Iterate the catalogue in bins of redshift.
Expand Down
8 changes: 4 additions & 4 deletions src/yaw/config/binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class BinningConfig(BaseConfig):
"""TODO"""

zbins: NDArray[np.float_] = field(
zbins: NDArray[np.float64] = field(
metadata=Parameter(
type=float,
nargs="*",
Expand Down Expand Up @@ -103,7 +103,7 @@ def is_manual(self) -> bool:
def create(
cls,
*,
zbins: NDArray[np.float_] | None = None,
zbins: NDArray[np.float64] | None = None,
zmin: float | None = None,
zmax: float | None = None,
zbin_num: int = DEFAULT.Binning.zbin_num,
Expand All @@ -124,7 +124,7 @@ def create(
that require cosmological distance computations.
Args:
zbins (:obj:`NDArray[np.float_]`):
zbins (:obj:`NDArray[np.float64]`):
Monotonically increasing redshift bin edges, including the upper
edge (ignored if ``zmin`` and ``zmax`` are provided).
zmin (:obj:`float`):
Expand Down Expand Up @@ -165,7 +165,7 @@ def create(

def modify(
self,
zbins: NDArray[np.float_] | None = DEFAULT.NotSet,
zbins: NDArray[np.float64] | None = DEFAULT.NotSet,
zmin: float | None = DEFAULT.NotSet,
zmax: float | None = DEFAULT.NotSet,
zbin_num: int = DEFAULT.NotSet,
Expand Down
Loading

0 comments on commit 97b8e18

Please sign in to comment.