Skip to content

Commit

Permalink
Prepare classes in sacc supportpy for move to sacc (#282)
Browse files Browse the repository at this point in the history
* Adjust pyccl calls to match pyccl 2.8.0 release
* Adjust names and add documentation
* Adjust examples and notebooks for name changes
* Apply black
* Attempted fix for CCL 2.8.0 upgrade
  • Loading branch information
marcpaterno committed Jun 12, 2023
1 parent 3b9799a commit 6900bf9
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 121 deletions.
18 changes: 9 additions & 9 deletions examples/cluster_number_counts/demo_sacc_for_clusters.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,16 @@
"s_count.add_tracer(\"cluster_survey\", survey_name, area)\n",
"\n",
"for i, z_bin in enumerate(zip(z_edges[:-1], z_edges[1:])):\n",
" z_lower, z_upper = z_bin\n",
" lower, upper = z_bin\n",
" bin_z_label = f\"bin_z_{i}\"\n",
" s_count.add_tracer(\"bin_z\", bin_z_label, z_lower, z_upper)\n",
" s_count.add_tracer(\"bin_z\", bin_z_label, lower, upper)\n",
" bin_z_labels.append(bin_z_label)\n",
"\n",
"for i, richness_bin in enumerate(zip(richness_edges[:-1], richness_edges[1:])):\n",
" richness_lower, richness_upper = richness_bin\n",
" lower, upper = richness_bin\n",
" bin_richness_label = f\"bin_rich_{i}\"\n",
" s_count.add_tracer(\n",
" \"bin_richness\", bin_richness_label, richness_lower, richness_upper\n",
" \"bin_richness\", bin_richness_label, lower, upper\n",
" )\n",
" bin_richness_labels.append(bin_richness_label)\n",
"\n",
Expand Down Expand Up @@ -404,16 +404,16 @@
"s_count2.add_tracer(\"cluster_survey\", survey_name, area)\n",
"\n",
"for i, z_bin in enumerate(zip(z_edges[:-1], z_edges[1:])):\n",
" z_lower, z_upper = z_bin\n",
" lower, upper = z_bin\n",
" bin_z_label = f\"bin_z_{i}\"\n",
" s_count2.add_tracer(\"bin_z\", bin_z_label, z_lower, z_upper)\n",
" s_count2.add_tracer(\"bin_z\", bin_z_label, lower, upper)\n",
" bin_z_labels.append(bin_z_label)\n",
"\n",
"for i, richness_bin in enumerate(zip(richness_edges[:-1], richness_edges[1:])):\n",
" richness_lower, richness_upper = richness_bin\n",
" lower, upper = richness_bin\n",
" bin_richness_label = f\"bin_rich_{i}\"\n",
" s_count2.add_tracer(\n",
" \"bin_richness\", bin_richness_label, richness_lower, richness_upper\n",
" \"bin_richness\", bin_richness_label, lower, upper\n",
" )\n",
" bin_richness_labels.append(bin_richness_label)\n",
"\n",
Expand Down Expand Up @@ -560,7 +560,7 @@
"source": [
"DS0 = [s_count2.get_data_points(data_type='cluster_shear', tracers=('NC_mock_redshift_richness', 'bin_z_1', 'bin_rich_0', f'bin_radius_{i}'))[0].value for i in np.arange(len(radius_edges)-1)]\n",
"DS2 = [s_count2.get_data_points(data_type='cluster_shear', tracers=('NC_mock_redshift_richness', 'bin_z_1', 'bin_rich_2', f'bin_radius_{i}'))[0].value for i in np.arange(len(radius_edges)-1)]\n",
"r_arr = [s_count2.get_tracer(f'bin_radius_{i}').r_center for i in np.arange(len(radius_edges)-1)]"
"r_arr = [s_count2.get_tracer(f'bin_radius_{i}').center for i in np.arange(len(radius_edges)-1)]"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,15 @@ def generate_sacc_file():
s_count.add_tracer("cluster_survey", survey_name, area)

for i, z_bin in enumerate(zip(z_edges[:-1], z_edges[1:])):
z_lower, z_upper = z_bin
lower, upper = z_bin
bin_z_label = f"bin_z_{i}"
s_count.add_tracer("bin_z", bin_z_label, z_lower, z_upper)
s_count.add_tracer("bin_z", bin_z_label, lower, upper)
bin_z_labels.append(bin_z_label)

for i, richness_bin in enumerate(zip(richness_edges[:-1], richness_edges[1:])):
richness_lower, richness_upper = richness_bin
lower, upper = richness_bin
bin_richness_label = f"rich_{i}"
s_count.add_tracer(
"bin_richness", bin_richness_label, richness_lower, richness_upper
)
s_count.add_tracer("bin_richness", bin_richness_label, lower, upper)
bin_richness_labels.append(bin_richness_label)

# pylint: disable-next=no-member
Expand Down
2 changes: 1 addition & 1 deletion firecrown/connector/cobaya/ccl.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize(self):

self.a_bg = np.linspace(0.1, 1.0, 50)
self.z_bg = 1.0 / self.a_bg - 1.0
self.z_Pk = np.arange(0.2, 6.0, 1)
self.z_Pk = np.arange(0.0, 6.0, 1)
self.Pk_kmax = 1.0

def get_param(self, p: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion firecrown/models/cluster_mass_rich_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def gen_bin_from_tracer(self, tracer: sacc.BaseTracer) -> ClusterMassArgument:
raise ValueError("Tracer must be a BinRichnessTracer")

return ClusterMassRichBinArgument(
self, self.logMl, self.logMu, tracer.richness_lower, tracer.richness_upper
self, self.logMl, self.logMu, tracer.lower, tracer.upper
)


Expand Down
6 changes: 3 additions & 3 deletions firecrown/models/cluster_redshift_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def gen_bins_by_array(self, z_bins: np.ndarray) -> List[ClusterRedshiftArgument]
# itertools.pairwise is only available in Python 3.10
# using zip instead
return [
ClusterRedshiftSpecArgument(z_lower, z_upper)
for z_lower, z_upper in zip(z_bins[:-1], z_bins[1:])
ClusterRedshiftSpecArgument(lower, upper)
for lower, upper in zip(z_bins[:-1], z_bins[1:])
]

def point_arg(self, z: float) -> ClusterRedshiftArgument:
Expand All @@ -65,7 +65,7 @@ def gen_bin_from_tracer(self, tracer: sacc.BaseTracer) -> ClusterRedshiftArgumen
if not isinstance(tracer, sacc_support.BinZTracer):
raise ValueError("Tracer must be a BinZTracer")

return ClusterRedshiftSpecArgument(tracer.z_lower, tracer.z_upper)
return ClusterRedshiftSpecArgument(tracer.lower, tracer.upper)


class ClusterRedshiftSpecArgument(ClusterRedshiftArgument):
Expand Down
102 changes: 50 additions & 52 deletions firecrown/sacc_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
class BinZTracer(BaseTracer, tracer_type="bin_z"): # type: ignore
"""A tracer for a single redshift bin."""

def __init__(self, name: str, z_lower: float, z_upper: float, **kwargs):
def __init__(self, name: str, lower: float, upper: float, **kwargs):
"""
Create a tracer corresponding to a single redshift bin.
:param name: The name of the tracer
:param z_lower: The lower bound of the redshift bin
:param z_upper: The upper bound of the redshift bin
:param lower: The lower bound of the redshift bin
:param upper: The upper bound of the redshift bin
"""
super().__init__(name, **kwargs)
self.z_lower = z_lower
self.z_upper = z_upper
self.lower = lower
self.upper = upper

def __eq__(self, other) -> bool:
"""Test for equality. If :python:`other` is not a
Expand All @@ -39,8 +39,8 @@ def __eq__(self, other) -> bool:
return False
return (
self.name == other.name
and self.z_lower == other.z_lower
and self.z_upper == other.z_upper
and self.lower == other.lower
and self.upper == other.upper
)

@classmethod
Expand All @@ -54,13 +54,13 @@ def to_tables(cls, instance_list):
:return: List with a single astropy table
"""

names = ["name", "quantity", "z_lower", "z_upper"]
names = ["name", "quantity", "lower", "upper"]

cols = [
[obj.name for obj in instance_list],
[obj.quantity for obj in instance_list],
[obj.z_lower for obj in instance_list],
[obj.z_upper for obj in instance_list],
[obj.lower for obj in instance_list],
[obj.upper for obj in instance_list],
]

table = Table(data=cols, names=names)
Expand All @@ -85,11 +85,9 @@ def from_tables(cls, table_list):
for row in table:
name = row["name"]
quantity = row["quantity"]
z_lower = row["z_lower"]
z_upper = row["z_upper"]
tracers[name] = cls(
name, quantity=quantity, z_lower=z_lower, z_upper=z_upper
)
lower = row["lower"]
upper = row["upper"]
tracers[name] = cls(name, quantity=quantity, lower=lower, upper=upper)
return tracers


Expand All @@ -105,23 +103,21 @@ def __eq__(self, other) -> bool:
return False
return (
self.name == other.name
and self.richness_lower == other.richness_lower
and self.richness_upper == other.richness_upper
and self.lower == other.lower
and self.upper == other.upper
)

def __init__(
self, name: str, richness_lower: float, richness_upper: float, **kwargs
):
def __init__(self, name: str, lower: float, upper: float, **kwargs):
"""
Create a tracer corresponding to a single richness bin.
:param name: The name of the tracer
:param richness_lower: The lower bound of the redshift bin
:param richness_upper: The upper bound of the redshift bin
:param lower: The lower bound of the redshift bin
:param upper: The upper bound of the redshift bin
"""
super().__init__(name, **kwargs)
self.richness_lower = richness_lower
self.richness_upper = richness_upper
self.lower = lower
self.upper = upper

@classmethod
def to_tables(cls, instance_list):
Expand All @@ -133,13 +129,13 @@ def to_tables(cls, instance_list):
:param instance_list: List of tracer instances
:return: List with a single astropy table
"""
names = ["name", "quantity", "richness_lower", "richness_upper"]
names = ["name", "quantity", "lower", "upper"]

cols = [
[obj.name for obj in instance_list],
[obj.quantity for obj in instance_list],
[obj.richness_lower for obj in instance_list],
[obj.richness_upper for obj in instance_list],
[obj.lower for obj in instance_list],
[obj.upper for obj in instance_list],
]

table = Table(data=cols, names=names)
Expand All @@ -164,13 +160,13 @@ def from_tables(cls, table_list):
for row in table:
name = row["name"]
quantity = row["quantity"]
richness_lower = row["richness_lower"]
richness_upper = row["richness_upper"]
lower = row["lower"]
upper = row["upper"]
tracers[name] = cls(
name,
quantity=quantity,
richness_lower=richness_lower,
richness_upper=richness_upper,
lower=lower,
upper=upper,
)
return tracers

Expand All @@ -187,25 +183,27 @@ def __eq__(self, other) -> bool:
return False
return (
self.name == other.name
and self.r_lower == other.r_lower
and self.r_center == other.r_center
and self.r_upper == other.r_upper
and self.lower == other.lower
and self.center == other.center
and self.upper == other.upper
)

def __init__(
self, name: str, r_lower: float, r_upper: float, r_center: float, **kwargs
):
def __init__(self, name: str, lower: float, upper: float, center: float, **kwargs):
"""
Create a tracer corresponding to a single radial bin.
:param name: The name of the tracer
:param r_lower: The lower bound of the radius bin
:param r_upper: The upper bound of the radius bin
:param lower: The lower bound of the radius bin
:param upper: The upper bound of the radius bin
:param center: The value to use if a single point-estimate is needed.
Note that :python:`center` need not be the midpoint between
:python:`lower` and :python:`upper`'.
"""
super().__init__(name, **kwargs)
self.r_lower = r_lower
self.r_upper = r_upper
self.r_center = r_center
self.lower = lower
self.upper = upper
self.center = center

@classmethod
def to_tables(cls, instance_list):
Expand All @@ -218,14 +216,14 @@ def to_tables(cls, instance_list):
:return: List with a single astropy table
"""

names = ["name", "quantity", "r_lower", "r_upper", "r_center"]
names = ["name", "quantity", "lower", "upper", "center"]

cols = [
[obj.name for obj in instance_list],
[obj.quantity for obj in instance_list],
[obj.r_lower for obj in instance_list],
[obj.r_upper for obj in instance_list],
[obj.r_center for obj in instance_list],
[obj.lower for obj in instance_list],
[obj.upper for obj in instance_list],
[obj.center for obj in instance_list],
]

table = Table(data=cols, names=names)
Expand All @@ -250,15 +248,15 @@ def from_tables(cls, table_list):
for row in table:
name = row["name"]
quantity = row["quantity"]
r_lower = row["r_lower"]
r_upper = row["r_upper"]
r_center = row["r_center"]
lower = row["lower"]
upper = row["upper"]
center = row["center"]
tracers[name] = cls(
name,
quantity=quantity,
r_lower=r_lower,
r_upper=r_upper,
r_center=r_center,
lower=lower,
upper=upper,
center=center,
)
return tracers

Expand Down
4 changes: 2 additions & 2 deletions tests/test_pt_systematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_pt_systematics(weak_lensing_source, number_counts_source, sacc_data):
mag_bias = 1.0

c_1, c_d, c_2 = pt.translate_IA_norm(
ccl_cosmo, z, a1=a_1, a1delta=a_d, a2=a_2, Om_m2_for_c2=False
ccl_cosmo, z=z, a1=a_1, a1delta=a_d, a2=a_2, Om_m2_for_c2=False
)

# Code that creates a Pk2D object:
Expand Down Expand Up @@ -267,7 +267,7 @@ def test_pt_mixed_systematics(sacc_data):
mag_bias = 1.0

c_1, c_d, c_2 = pt.translate_IA_norm(
ccl_cosmo, z, a1=a_1, a1delta=a_d, a2=a_2, Om_m2_for_c2=False
ccl_cosmo, z=z, a1=a_1, a1delta=a_d, a2=a_2, Om_m2_for_c2=False
)

# Code that creates a Pk2D object:
Expand Down
Loading

0 comments on commit 6900bf9

Please sign in to comment.