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

Move exceptions to new module #1845

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
162 changes: 162 additions & 0 deletions src/pint/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
__all__ = [
"TimingModelError",
"MissingParameter",
"MissingTOAs",
"MissingBinaryError",
"UnknownBinaryModel",
]


# originally from fitter.py
class DegeneracyWarning(UserWarning):
pass


class ConvergenceFailure(ValueError):
pass


class MaxiterReached(ConvergenceFailure):
pass


class StepProblem(ConvergenceFailure):
pass
Comment on lines +26 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add docstrings explaining what each of these exceptions mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I necessarily can, since I didn't write them. I could probably infer some from usage but that would take a while.



class CorrelatedErrors(ValueError):
def __init__(self, model):
trouble_components = [
c.__class__.__name__
for c in model.NoiseComponent_list
if c.introduces_correlated_errors
]
super().__init__(
f"Model has correlated errors and requires a GLS-based fitter; "
f"remove {trouble_components} if you want to use WLS"
)
self.trouble_components = trouble_components


# from timing_model.py
class MissingTOAs(ValueError):
"""Some parameter does not describe any TOAs."""

def __init__(self, parameter_names):
if isinstance(parameter_names, str):
parameter_names = [parameter_names]
if len(parameter_names) == 1:
msg = f"Parameter {parameter_names[0]} does not correspond to any TOAs: you might need to run `model.find_empty_masks(toas, freeze=True)`"
elif len(parameter_names) > 1:
msg = f"Parameters {' '.join(parameter_names)} do not correspond to any TOAs: you might need to run `model.find_empty_masks(toas, freeze=True)`"
else:
raise ValueError("Incorrect attempt to construct MissingTOAs")
super().__init__(msg)
self.parameter_names = parameter_names


class PropertyAttributeError(ValueError):
pass


class TimingModelError(ValueError):
"""Generic base class for timing model errors."""

pass


class MissingParameter(TimingModelError):
"""A required model parameter was not included.

Parameters
----------
module
name of the model class that raised the error
param
name of the missing parameter
msg
additional message

"""

def __init__(self, module, param, msg=None):
super().__init__(msg)
self.module = module
self.param = param
self.msg = msg

def __str__(self):
result = f"{self.module}.{self.param}"
if self.msg is not None:
result += "\n " + self.msg
return result


class AliasConflict(TimingModelError):
"""If the same alias is used for different parameters."""

pass


class UnknownParameter(TimingModelError):
"""Signal that a parameter name does not match any PINT parameters and their aliases."""

pass


class UnknownBinaryModel(TimingModelError):
"""Signal that the par file requested a binary model not in PINT."""

def __init__(self, message, suggestion=None):
super().__init__(message)
self.suggestion = suggestion

def __str__(self):
base_message = super().__str__()
if self.suggestion:
return f"{base_message} Perhaps use {self.suggestion}?"
return base_message


class MissingBinaryError(TimingModelError):
"""Error for missing BINARY parameter."""

pass


# from utils.py
class PINTPrecisionError(RuntimeError):
pass


class PrefixError(ValueError):
pass


# from models.parameter.py
class InvalidModelParameters(ValueError):
pass


# models.model_builder.py
class ComponentConflict(ValueError):
"""Error for multiple components can be select but no other indications."""


# observatories.__init__.py
class ClockCorrectionError(RuntimeError):
"""Unspecified error doing clock correction."""

pass


class NoClockCorrections(ClockCorrectionError):
"""Clock corrections are expected but none are available."""

pass


class ClockCorrectionOutOfRange(ClockCorrectionError):
"""Clock corrections are available but the requested time is not covered."""

pass
37 changes: 7 additions & 30 deletions src/pint/fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@
from pint.residuals import Residuals, WidebandTOAResiduals
from pint.toa import TOAs
from pint.utils import FTest, normalize_designmatrix
from pint.exceptions import (
DegeneracyWarning,
ConvergenceFailure,
MaxiterReached,
StepProblem,
CorrelatedErrors,
)


__all__ = [
Expand Down Expand Up @@ -167,22 +174,6 @@ def __get__(self, instance, owner=None):
return val


class DegeneracyWarning(UserWarning):
pass


class ConvergenceFailure(ValueError):
pass


class MaxiterReached(ConvergenceFailure):
pass


class StepProblem(ConvergenceFailure):
pass


class Fitter:
"""Base class for objects encapsulating fitting problems.

Expand Down Expand Up @@ -888,20 +879,6 @@ def covariance_matrix(self):
return self.parameter_covariance_matrix


class CorrelatedErrors(ValueError):
def __init__(self, model):
trouble_components = [
c.__class__.__name__
for c in model.NoiseComponent_list
if c.introduces_correlated_errors
]
super().__init__(
f"Model has correlated errors and requires a GLS-based fitter; "
f"remove {trouble_components} if you want to use WLS"
)
self.trouble_components = trouble_components


class ModelState:
"""Record a model state and cache calculations

Expand Down
3 changes: 2 additions & 1 deletion src/pint/models/absolute_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import pint.toa as toa
from pint.models.parameter import MJDParameter, floatParameter, strParameter
from pint.models.timing_model import MissingParameter, PhaseComponent
from pint.models.timing_model import PhaseComponent
from pint.exceptions import MissingParameter


class AbsPhase(PhaseComponent):
Expand Down
3 changes: 2 additions & 1 deletion src/pint/models/astrometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
strParameter,
)
import pint.toa
from pint.models.timing_model import DelayComponent, MissingParameter
from pint.models.timing_model import DelayComponent
from pint.pulsar_ecliptic import OBL, PulsarEcliptic
from pint.utils import add_dummy_distance, remove_dummy_distance
from pint.exceptions import MissingParameter

astropy_version = sys.modules["astropy"].__version__
mas_yr = u.mas / u.yr
Expand Down
2 changes: 1 addition & 1 deletion src/pint/models/binary_bt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pint.models.pulsar_binary import PulsarBinary
from pint.models.stand_alone_psr_binaries.BT_model import BTmodel
from pint.models.stand_alone_psr_binaries.BT_piecewise import BTpiecewise
from pint.models.timing_model import MissingParameter
from pint.exceptions import MissingParameter
import astropy.units as u
import astropy.constants as consts
from pint import ls
Expand Down
2 changes: 1 addition & 1 deletion src/pint/models/binary_ddk.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pint.models.binary_dd import BinaryDD
from pint.models.parameter import boolParameter, floatParameter, funcParameter
from pint.models.stand_alone_psr_binaries.DDK_model import DDKmodel
from pint.models.timing_model import MissingParameter, TimingModelError
from pint.exceptions import MissingParameter, TimingModelError


def _convert_kin(kin):
Expand Down
2 changes: 1 addition & 1 deletion src/pint/models/binary_ell1.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pint.models.stand_alone_psr_binaries.ELL1_model import ELL1model
from pint.models.stand_alone_psr_binaries.ELL1H_model import ELL1Hmodel
from pint.models.stand_alone_psr_binaries.ELL1k_model import ELL1kmodel
from pint.models.timing_model import MissingParameter
from pint.exceptions import MissingParameter
from pint.utils import taylor_horner_deriv
from pint import Tsun

Expand Down
3 changes: 2 additions & 1 deletion src/pint/models/chromatic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from warnings import warn
import numpy as np
import astropy.units as u
from pint.models.timing_model import DelayComponent, MissingParameter
from pint.models.timing_model import DelayComponent
from pint.models.parameter import floatParameter, prefixParameter, MJDParameter
from pint.utils import split_prefixed_name, taylor_horner, taylor_horner_deriv
from pint import DMconst
from pint.exceptions import MissingParameter
from astropy.time import Time

cmu = u.pc / u.cm**3 / u.MHz**2
Expand Down
2 changes: 1 addition & 1 deletion src/pint/models/cmwavex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from warnings import warn

from pint.models.parameter import MJDParameter, prefixParameter
from pint.models.timing_model import MissingParameter
from pint.exceptions import MissingParameter
from pint.models.chromatic_model import Chromatic, cmu
from pint import DMconst

Expand Down
3 changes: 2 additions & 1 deletion src/pint/models/dispersion_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
prefixParameter,
maskParameter,
)
from pint.models.timing_model import DelayComponent, MissingParameter, MissingTOAs
from pint.models.timing_model import DelayComponent
from pint.toa_select import TOASelect
from pint.utils import (
split_prefixed_name,
taylor_horner,
taylor_horner_deriv,
)
from pint import DMconst
from pint.exceptions import MissingParameter, MissingTOAs

# This value is cited from Duncan Lorimer, Michael Kramer, Handbook of Pulsar
# Astronomy, Second edition, Page 86, Note 1
Expand Down
2 changes: 1 addition & 1 deletion src/pint/models/dmwavex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from warnings import warn

from pint.models.parameter import MJDParameter, prefixParameter
from pint.models.timing_model import MissingParameter
from pint.exceptions import MissingParameter
from pint.models.dispersion_model import Dispersion
from pint import DMconst, dmu

Expand Down
3 changes: 2 additions & 1 deletion src/pint/models/frequency_dependent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import numpy as np

from pint.models.parameter import prefixParameter
from pint.models.timing_model import DelayComponent, MissingParameter
from pint.models.timing_model import DelayComponent
from pint.exceptions import MissingParameter


class FD(DelayComponent):
Expand Down
3 changes: 2 additions & 1 deletion src/pint/models/glitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from loguru import logger as log

from pint.models.parameter import prefixParameter
from pint.models.timing_model import MissingParameter, PhaseComponent
from pint.models.timing_model import PhaseComponent
from pint.utils import split_prefixed_name
from pint.exceptions import MissingParameter


class Glitch(PhaseComponent):
Expand Down
3 changes: 2 additions & 1 deletion src/pint/models/ifunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import numpy as np

from pint.models.parameter import floatParameter, prefixParameter
from pint.models.timing_model import PhaseComponent, MissingParameter
from pint.models.timing_model import PhaseComponent
from pint.exceptions import MissingParameter


class IFunc(PhaseComponent):
Expand Down
17 changes: 8 additions & 9 deletions src/pint/models/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
AllComponents,
TimingModel,
ignore_prefix,
UnknownBinaryModel,
UnknownParameter,
TimingModelError,
MissingBinaryError,
ignore_params,
ignore_prefix,
)
from pint.toa import TOAs, get_TOAs
from pint.utils import (
PrefixError,
interesting_lines,
lines_of,
split_prefixed_name,
Expand All @@ -33,6 +28,14 @@
from pint.models.tcb_conversion import convert_tcb_tdb
from pint.models.binary_ddk import _convert_kin, _convert_kom
from pint.types import file_like, quantity_like
from pint.exceptions import (
PrefixError,
ComponentConflict,
UnknownParameter,
UnknownBinaryModel,
TimingModelError,
MissingBinaryError,
)

__all__ = ["ModelBuilder", "get_model", "get_model_and_toas"]

Expand All @@ -52,10 +55,6 @@
]


class ComponentConflict(ValueError):
"""Error for multiple components can be select but no other indications."""


def parse_parfile(parfile):
"""Function for parsing .par file or .par style StringIO.

Expand Down
Loading
Loading