Skip to content

Commit

Permalink
Move generate_bin_centers to generators
Browse files Browse the repository at this point in the history
  • Loading branch information
marcpaterno committed Sep 18, 2024
1 parent 5fc0fb6 commit fc3070f
Showing 3 changed files with 31 additions and 29 deletions.
26 changes: 25 additions & 1 deletion firecrown/generators/two_point.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@

from typing import Annotated
from pydantic import BaseModel, Field, model_validator

import numpy as np
import numpy.typing as npt

@@ -68,3 +67,28 @@ def log_linear_ells(
return LogLinearElls(
minimum=minimum, midpoint=midpoint, maximum=maximum, n_log=n_log
).generate()


def generate_bin_centers(
*, minimum: float, maximum: float, n: int, binning: str = "log"
) -> npt.NDArray[np.float64]:
"""Return the centers of bins that span the range from minimum to maximum.
If binning is 'log', this will generate logarithmically spaced bins; if
binning is 'lin', this will generate linearly spaced bins.
:param minimum: The low edge of the first bin.
:param maximum: The high edge of the last bin.
:param n: The number of bins.
:param binning: Either 'log' or 'lin'.
:return: The centers of the bins.
"""
match binning:
case "log":
edges = np.logspace(np.log10(minimum), np.log10(maximum), n + 1)
return np.sqrt(edges[1:] * edges[:-1])
case "lin":
edges = np.linspace(minimum, maximum, n + 1)
return (edges[1:] + edges[:-1]) / 2.0
case _:
raise ValueError(f"Unrecognized binning: {binning}")
31 changes: 5 additions & 26 deletions firecrown/likelihood/two_point.py
Original file line number Diff line number Diff line change
@@ -16,7 +16,11 @@
# firecrown is needed for backward compatibility; remove support for deprecated
# directory structure is removed.
import firecrown # pylint: disable=unused-import # noqa: F401
from firecrown.generators.two_point import ELL_FOR_XI_DEFAULTS, log_linear_ells
from firecrown.generators.two_point import (
ELL_FOR_XI_DEFAULTS,
log_linear_ells,
generate_bin_centers,
)
from firecrown.likelihood.source import Source, Tracer
from firecrown.likelihood.weak_lensing import (
WeakLensingFactory,
@@ -66,31 +70,6 @@
}


def generate_bin_centers(
*, minimum: float, maximum: float, n: int, binning: str = "log"
) -> npt.NDArray[np.float64]:
"""Return the centers of bins that span the range from minimum to maximum.
If binning is 'log', this will generate logarithmically spaced bins; if
binning is 'lin', this will generate linearly spaced bins.
:param minimum: The low edge of the first bin.
:param maximum: The high edge of the last bin.
:param n: The number of bins.
:param binning: Either 'log' or 'lin'.
:return: The centers of the bins.
"""
match binning:
case "log":
edges = np.logspace(np.log10(minimum), np.log10(maximum), n + 1)
return np.sqrt(edges[1:] * edges[:-1])
case "lin":
edges = np.linspace(minimum, maximum, n + 1)
return (edges[1:] + edges[:-1]) / 2.0
case _:
raise ValueError(f"Unrecognized binning: {binning}")


# @functools.lru_cache(maxsize=128)
def _cached_angular_cl(cosmo, tracers, ells, p_of_k_a=None):
return pyccl.angular_cl(
3 changes: 1 addition & 2 deletions tests/likelihood/gauss_family/statistic/test_two_point.py
Original file line number Diff line number Diff line change
@@ -21,7 +21,6 @@
)
from firecrown.likelihood.statistic import TheoryVector
from firecrown.likelihood.two_point import (
generate_bin_centers,
TwoPoint,
TracerNames,
TRACER_NAMES_TOTAL,
@@ -31,7 +30,7 @@
WeakLensingFactory,
NumberCountsFactory,
)
from firecrown.generators.two_point import log_linear_ells
from firecrown.generators.two_point import log_linear_ells, generate_bin_centers
from firecrown.metadata_types import (
Galaxies,
InferredGalaxyZDist,

0 comments on commit fc3070f

Please sign in to comment.