Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

185 more pylint cleanup #186

Merged
merged 22 commits into from
Oct 10, 2022
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
6 changes: 1 addition & 5 deletions docs/non-developer-mode-example/des_y1_3x2pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@
"""
We load the correct SACC file.
"""
saccfile = os.path.expanduser(
os.path.expandvars(
"des_y1_3x2pt_sacc_data.fits"
)
)
saccfile = os.path.expanduser(os.path.expandvars("des_y1_3x2pt_sacc_data.fits"))
sacc_data = sacc.Sacc.load_fits(saccfile)

"""
Expand Down
1 change: 0 additions & 1 deletion examples/cosmicshear/cosmicshear.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,3 @@
be used to compute the likelihood.
"""
likelihood = lk

43 changes: 30 additions & 13 deletions firecrown/likelihood/gauss_family/gauss_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from __future__ import annotations
from typing import List, Optional
from typing import final
from abc import abstractmethod

import numpy as np
import scipy.linalg

Expand All @@ -20,7 +22,6 @@
from ...updatable import UpdatableCollection
from .statistic.statistic import Statistic
from ...parameters import ParamsMap, RequiredParameters
from abc import abstractmethod


class GaussFamily(Likelihood):
Expand Down Expand Up @@ -58,42 +59,59 @@ def read(self, sacc_data: sacc.Sacc) -> None:
@final
def compute_chisq(self, cosmo: pyccl.Cosmology) -> float:
"""Calculate and return the chi-squared for the given cosmology."""

r = []
residuals = []
theory_vector = []
data_vector = []
for stat in self.statistics:
data, theory = stat.compute(cosmo)
r.append(np.atleast_1d(data - theory))
residuals.append(np.atleast_1d(data - theory))
theory_vector.append(np.atleast_1d(theory))
data_vector.append(np.atleast_1d(data))

r = np.concatenate(r, axis=0)
residuals = np.concatenate(residuals, axis=0)
self.predicted_data_vector = np.concatenate(theory_vector)
self.measured_data_vector = np.concatenate(data_vector)
x = scipy.linalg.solve_triangular(self.cholesky, r, lower=True)
# pylint: disable-next=C0103
x = scipy.linalg.solve_triangular(self.cholesky, residuals, lower=True)
return np.dot(x, x)

@final
def _update(self, params: ParamsMap):
def _update(self, params: ParamsMap) -> None:
"""Implementation of the Likelihood interface method _update.

This updates all statistics and calls teh abstract method
_update_gaussian_family."""
self.statistics.update(params)
self._update_gaussian_family(params)

@final
def _reset(self):
def _reset(self) -> None:
"""Implementation of Likelihood interface method _reset.

This resets all statistics and calls the abstract method
_reset_gaussian_family."""
self._reset_gaussian_family()
self.statistics.reset()

@abstractmethod
def _update_gaussian_family(self, params: ParamsMap):
pass
def _update_gaussian_family(self, params: ParamsMap) -> None:
"""Abstract method to update GaussianFamily state. Must be implemented by all
subclasses."""

@abstractmethod
def _reset_gaussian_family(self, params: ParamsMap):
pass
def _reset_gaussian_family(self) -> None:
"""Abstract method to reset GaussianFamily state. Must be implemented by all
subclasses."""

@final
def required_parameters(self) -> RequiredParameters:
"""Return a RequiredParameters object containing the information for
this Updatable.

This includes the required parameters for all statistics, as well as those
for the derived class.

Derived classes must implement required_parameters_gaussian_family."""
stats_rp = self.statistics.required_parameters()
stats_rp = self.required_parameters_gaussian_family() + stats_rp

Expand All @@ -102,4 +120,3 @@ def required_parameters(self) -> RequiredParameters:
@abstractmethod
def required_parameters_gaussian_family(self):
"""Required parameters for GaussFamily subclasses."""
pass
10 changes: 1 addition & 9 deletions firecrown/likelihood/gauss_family/statistic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
"""

Gaussian Family Statistic Package
=================================

This package provides statistics objects to be used by Gaussian Family
"""This package provides statistics objects to be used by Gaussian Family
likelihoods.


"""

# flake8: noqa
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
"""

Two Point Source Package
========================

This package contains the base class :class:`Source` for :class:`TwoPoint`
"""This package contains the base class :class:`Source` for :class:`TwoPoint`
sources and implementations.

"""

# flake8: noqa
40 changes: 20 additions & 20 deletions firecrown/likelihood/gauss_family/statistic/source/number_counts.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"""

Number Counts Source Module
===========================

The classe in this file define ...
"""Number counts source and systematics

"""

Expand All @@ -26,10 +21,10 @@

@dataclass(frozen=True)
class NumberCountsArgs:
"""Class for weak lensing tracer builder argument."""
"""Class for number counts tracer builder argument."""

scale: float
z: np.ndarray
z: np.ndarray # pylint: disable-msg=invalid-name
dndz: np.ndarray
bias: np.ndarray
mag_bias: np.ndarray
Expand Down Expand Up @@ -60,9 +55,6 @@ class LinearBiasSystematic(NumberCountsSystematic):
z_piv : str
The name of the pivot redshift parameter for the linear bias.

Methods
-------
apply : apply the systematic to a source
"""

params_names = ["alphaz", "alphag", "z_piv"]
Expand All @@ -85,7 +77,9 @@ def _update(self, params: ParamsMap):

@final
def _reset(self) -> None:
pass
"""Reset this systematic.

This implementation has nothing to do."""

@final
def required_parameters(self) -> RequiredParameters:
Expand Down Expand Up @@ -161,7 +155,9 @@ def _update(self, params: ParamsMap):

@final
def _reset(self) -> None:
pass
"""Reset this systematic.

This implementation has nothing to do."""

@final
def required_parameters(self) -> RequiredParameters:
Expand All @@ -183,13 +179,13 @@ def apply(
"""

z_bar = self.z_c + self.z_m * (self.r_lim - 24.0)
z = tracer_arg.z
# The slope of log(n_tot(z,r_lim)) with respect to r_lim
# where n_tot(z,r_lim) is the luminosity function after using fit (C.1)
# pylint: disable-next=invalid-name
s = (
self.eta / self.r_lim
- 3.0 * self.z_m / z_bar
+ 1.5 * self.z_m * np.power(z / z_bar, 1.5) / z_bar
+ 1.5 * self.z_m * np.power(tracer_arg.z / z_bar, 1.5) / z_bar
)

return NumberCountsArgs(
Expand Down Expand Up @@ -221,7 +217,9 @@ def _update(self, params: ParamsMap):

@final
def _reset(self) -> None:
pass
"""Reset this systematic.

This implementation has nothing to do."""

@final
def required_parameters(self) -> RequiredParameters:
Expand All @@ -247,6 +245,8 @@ def apply(self, cosmo: pyccl.Cosmology, tracer_arg: NumberCountsArgs):


class NumberCounts(Source):
"""Source class for number counts."""

params_names = ["bias", "mag_bias"]
bias: float
mag_bias: Optional[float]
Expand All @@ -257,10 +257,10 @@ class NumberCounts(Source):
def __init__(
self,
*,
sacc_tracer,
has_rsd=False,
has_mag_bias=False,
scale=1.0,
sacc_tracer: str,
has_rsd: bool = False,
has_mag_bias: bool = False,
scale: float = 1.0,
systematics: Optional[List[NumberCountsSystematic]] = None,
):
super().__init__()
Expand Down
39 changes: 23 additions & 16 deletions firecrown/likelihood/gauss_family/statistic/source/source.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"""

Two Point Statistic Source Module
=================================

The class in this file define ...
"""Abstract base classes for GaussianFamily statistics.

"""

Expand Down Expand Up @@ -53,41 +48,53 @@ def read(self, sacc_data: sacc.Sacc):

@abstractmethod
def _read(self, sacc_data: sacc.Sacc):
"""Read the data for this source from the SACC file."""
pass
"""Abstract method to read the data for this source from the SACC file."""

@abstractmethod
def _update_source(self, params: ParamsMap):
pass
"""Abstract method to update the source from the given ParamsMap."""

@abstractmethod
def _reset_source(self):
pass
"""Abstract method to reset the source."""

@final
def _update(self, params: ParamsMap):
"""Implementation of Updatable interface method `_update`.

This clears the current hash and tracer, and calls the abstract method
`_update_source`, which must be implemented in all subclasses."""
self.cosmo_hash = None
self.tracer = None
self._update_source(params)

@final
def _reset(self) -> None:
"""Implementation of the Updatable interface method `_reset`.

This calls the abstract method `_reset_source`, which must be implemented by
all subclasses."""
self._reset_source()

@abstractmethod
def get_scale(self) -> float:
pass
"""Abstract method to return the scale for this `Source`."""

@abstractmethod
def create_tracer(self, cosmo: pyccl.Cosmology):
pass
"""Abstract method to create a tracer for this `Source`, for the given
cosmology."""

@final
def get_tracer(self, cosmo: pyccl.Cosmology) -> pyccl.tracers.Tracer:
"""Return the tracer for the given cosmology.

This method caches its result, so if called a second time with the same
cosmology, no calculation needs to be done."""
cur_hash = hash(cosmo)
if hasattr(self, "cosmo_hash") and self.cosmo_hash == cur_hash:
return self.tracer
else:
self.tracer, _ = self.create_tracer(cosmo)
self.cosmo_hash = cur_hash
return self.tracer

self.tracer, _ = self.create_tracer(cosmo)
self.cosmo_hash = cur_hash
return self.tracer
Loading