Skip to content

Commit

Permalink
Merge pull request #202 from threeML/cosmology
Browse files Browse the repository at this point in the history
Cosmology
  • Loading branch information
grburgess authored Oct 11, 2023
2 parents 0410d1c + 84d919c commit 4a91c54
Show file tree
Hide file tree
Showing 12 changed files with 517 additions and 290 deletions.
2 changes: 1 addition & 1 deletion astromodels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

if os.environ.get("ASTROMODELS_DEBUG", None) is None:

from .utils.configuration import astromodels_config
from .utils.configuration import astromodels_config, show_configuration
from .core.memoization import use_astromodels_memoization
from .core.model import Model
from .core.model_parser import clone_model, load_model
Expand Down
2 changes: 1 addition & 1 deletion astromodels/core/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import contextlib
import copy
import warnings
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple

import astropy.units as u
import numpy as np
Expand Down
37 changes: 13 additions & 24 deletions astromodels/core/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def __init__(
defer: bool = False,
eval_func: Optional[str] = None,
):

# Make this a node

Node.__init__(self, name)
Expand All @@ -35,8 +34,9 @@ def __init__(
self._eval_func: Optional[str] = eval_func

if (value is None) and (not self._defer):

log.error(f"property {name} was given no initial value but is NOT deferred")
log.error(
f"property {name} was given no initial value but is NOT deferred"
)

# now we set the value

Expand All @@ -51,7 +51,7 @@ def _get_value(self) -> Any:
Return current parameter value
"""

log.debug(
log.debug_node(
f"accessing the property {self.name} with value {self._internal_value}"
)

Expand All @@ -63,14 +63,11 @@ def _set_value(self, new_value) -> None:
"""

if (self._defer) and (new_value is None):

# this is ok
pass

elif self._allowed_values is not None:

if new_value not in self._allowed_values:

log.error(
f"{self.name} can only take the values {','.join(self._allowed_values)} not {new_value}"
)
Expand All @@ -84,31 +81,29 @@ def _set_value(self, new_value) -> None:
# on the parent

if (self._internal_value == "_tmp") and self._defer:

# do not execute in this mode

return

if self._eval_func is not None:

# if there is a parent
if self._parent is not None:

if self._parent.name == "composite":
# ok, we have a composite function

func_idx = int(self._name.split("_")[-1]) - 1

log.debug(f"{self._name} has a composite parent and")
log.debug(f"is being executed on func idx {func_idx}")
log.debug(
log.debug_node(f"{self._name} has a composite parent and")
log.debug_node(f"is being executed on func idx {func_idx}")
log.debug_node(
f"and the parent has {len(self._parent._functions)} functions"
)

getattr(self._parent._functions[func_idx], str(self._eval_func))()
getattr(
self._parent._functions[func_idx], str(self._eval_func)
)()

else:

getattr(self._parent, str(self._eval_func))()

# other wise this will run when the parent is set
Expand All @@ -120,7 +115,6 @@ def _set_value(self, new_value) -> None:
)

def _set_parent(self, parent):

# we intecept here becuase we want
# to make sure the eval works

Expand Down Expand Up @@ -154,7 +148,6 @@ def duplicate(self) -> "FunctionProperty":
return new_property

def _repr__base(self, rich_output): # pragma: no cover

raise NotImplementedError(
"You need to implement this for the actual Property class"
)
Expand All @@ -171,11 +164,9 @@ def _to_python_type(variable):
# Assume variable is a np.array, fall back to the case where variable is already a primitive type

try:

return variable.item()

except AttributeError:

return variable

def to_dict(self, minimal=False) -> Dict[str, Any]:
Expand All @@ -184,16 +175,16 @@ def to_dict(self, minimal=False) -> Dict[str, Any]:
data = collections.OrderedDict()

if minimal:

# In the minimal representation we just output the value

data["value"] = self._to_python_type(self.value)

else:

# In the complete representation we output everything is needed to re-build the object

data["value"] = str(self.value)
data["value"] = (
self.value if type(self.value) == bool else str(self.value)
)
data["desc"] = str(self._desc)
data["allowed values"] = self._to_python_type(self._allowed_values)
data["defer"] = self._to_python_type(self._defer)
Expand All @@ -212,7 +203,6 @@ def __init__(
defer: bool = False,
eval_func: Optional[str] = None,
):

super(FunctionProperty, self).__init__(
name=name,
desc=desc,
Expand All @@ -223,7 +213,6 @@ def __init__(
)

def _repr__base(self, rich_output=False):

representation = (
f"Property {self.name} = {self.value}\n"
f"(allowed values = {'all' if self._allowed_values is None else ' ,'.join(self._allowed_values)})"
Expand Down
Loading

0 comments on commit 4a91c54

Please sign in to comment.