Skip to content

Commit

Permalink
Added factory pattern method to the integ configs
Browse files Browse the repository at this point in the history
  • Loading branch information
MetinSa committed Aug 22, 2021
1 parent 67bf8d9 commit 943dd14
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 74 deletions.
2 changes: 1 addition & 1 deletion zodipy/_emissivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


@dataclass
class Emissivity:
class Emissivities:
"""Class containing emissivity fits for the Zodiacal components.
Attributes
Expand Down
74 changes: 35 additions & 39 deletions zodipy/_integration.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
from typing import Callable
from typing import Callable, Tuple, Dict

import numpy as np

ZERO = np.finfo(float).eps
RADIAL_CUTOFF = 6

_ZERO = np.finfo(float).eps


class IntegrationConfig:
"""Config object for the LOS/shell integration in the simulation.
Attributes
----------
R_max
Maximum distance from the observer for which to evaluate the
dust density.
n
Number of shells for which to evaluate the dust density.
integator
Function which performs the integration.
"""
"""Configuration for the integration strategies in the simulations."""

def __init__(
self,
R_max: float,
n:int,
integrator: Callable[[float, float, int], np.ndarray] = np.trapz
):
self.R = np.expand_dims(np.linspace(ZERO, R_max, n), axis=1)
self.R = np.expand_dims(np.linspace(_ZERO, R_max, n), axis=1)
self.dR = np.diff(self.R)
self.integrator = integrator


DEFAULT = {
'cloud': IntegrationConfig(RADIAL_CUTOFF, 250),
'band1': IntegrationConfig(RADIAL_CUTOFF, 50),
'band2': IntegrationConfig(RADIAL_CUTOFF, 50),
'band3': IntegrationConfig(RADIAL_CUTOFF, 50),
'ring': IntegrationConfig(2.25, 50),
'feature': IntegrationConfig(1, 50),
}

HIGH = {
'cloud': IntegrationConfig(RADIAL_CUTOFF, 500),
'band1': IntegrationConfig(RADIAL_CUTOFF, 500),
'band2': IntegrationConfig(RADIAL_CUTOFF, 500),
'band3': IntegrationConfig(RADIAL_CUTOFF, 500),
'ring': IntegrationConfig(2.25, 200),
'feature': IntegrationConfig(1, 200),
}


INTEGRATION_CONFIGS = {
'default' : DEFAULT,
'high' : HIGH,
}
class IntegrationConfigFactory:
"""Factory responsible for registring and book-keeping integration configs."""

def __init__(self) -> None:
self._configs = {}

def register_config(
self,
name: str,
components: Dict[str, Tuple[float,int]]
) -> None:
"""Initializes and stores an integration config."""

config = {}
for key, value in components.items():
config[key] = IntegrationConfig(*value)

self._configs[name] = config

def get_config(self, name: str) -> IntegrationConfig:
"""Returns a registered config."""

config = self._configs.get(name)
if config is None:
raise ValueError(
f'Config {name} is not registered. Available configs are '
f'{list(self._configs.keys())}'
)

return config
16 changes: 9 additions & 7 deletions zodipy/_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from typing import Iterable, Dict
from typing import Iterable, Dict, Union, Tuple

from zodipy._emissivity import Emissivity
from zodipy._emissivity import Emissivities
from zodipy.components import BaseComponent, Cloud, Band, Ring, Feature


Expand All @@ -10,11 +10,11 @@ class InterplanetaryDustModel:
"""Data class representing an Interplanetary dust model."""

components: Dict[str, BaseComponent]
emissivities: Emissivity
emissivities: Emissivities


class ModelFactory:
"""Class that is responsible for registring and book-keeping models."""
"""Factory responsible for registring and book-keeping models."""

def __init__(self) -> None:
self._models = {}
Expand All @@ -24,20 +24,20 @@ def register_model(
name: str,
components: Iterable[str],
parameters: Dict[str, Dict[str, float]],
emissivities: Emissivity
emissivities: Dict[str, Union[Tuple[float], Dict[str, Tuple[float]]]]
) -> None:
"""Initializes and stores an IPD model."""

model = init_model(components, parameters, emissivities)
self._models[name] = model

def get_model(self, name: str) -> InterplanetaryDustModel:
"""Returns a model from the list of registered models."""
"""Returns a registered model."""

model = self._models.get(name)
if model is None:
raise ValueError(
f'model {name} is not registered. Available models are'
f'model {name} is not registered. Available models are '
f'{list(self._models.keys())}'
)

Expand All @@ -57,6 +57,8 @@ def init_model(components, parameters, emissivities):
comp_type = Feature
initialized_components[comp] = comp_type(**parameters[comp])

emissivities = Emissivities(**emissivities)

return InterplanetaryDustModel(
components=initialized_components,
emissivities=emissivities
Expand Down
31 changes: 12 additions & 19 deletions zodipy/emissivities.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
from zodipy._emissivity import Emissivity

_PLANCK_FREQUENCIES = (100.0, 143.0, 217.0, 353.0, 545.0, 857.0)


PLANCK_2013 = Emissivity(
frequencies=_PLANCK_FREQUENCIES,
components=dict(
PLANCK_2013 = {
'frequencies' : _PLANCK_FREQUENCIES,
'components' : dict(
cloud=(0.003, -0.014, 0.031, 0.168, 0.223, 0.301),
band1=(1.129, 1.463, 2.024, 2.035, 2.235, 1.777),
band2=(0.674, 0.530, 0.338, 0.436, 0.718, 0.716),
band3=(1.106, 1.794, 2.507, 2.400, 3.193, 2.870),
ring=(0.163, -0.252, -0.185, -0.211, 0.591, 0.578),
feature=(0.252, -0.002, 0.243, 0.676, -0.182, 0.423)
)
)


PLANCK_2015 = Emissivity(
frequencies=_PLANCK_FREQUENCIES,
components=dict(
}
PLANCK_2015 = {
'frequencies' : _PLANCK_FREQUENCIES,
'components' : dict(
cloud=(0.012, 0.022, 0.051, 0.106, 0.167, 0.256),
band1=(1.02, 1.23, 1.30, 1.58, 1.74, 2.06),
band2=(0.08, 0.15, 0.15, 0.39, 0.54, 0.85),
band3=(0.72, 1.16, 1.27, 1.88, 2.54, 3.37),
ring=None,
feature=None,
)
)


PLANCK_2018 = Emissivity(
frequencies=_PLANCK_FREQUENCIES,
components=dict(
}
PLANCK_2018 = {
'frequencies' : _PLANCK_FREQUENCIES,
'components' : dict(
cloud=(0.018, 0.020, 0.042, 0.082, 0.179, 0.304),
band1=(0.54, 1.00, 1.11, 1.52, 1.47, 1.58),
band2=(0.07, 0.17, 0.21, 0.35, 0.49, 0.70),
band3=(0.19, 0.84, 1.12, 1.77, 1.84, 2.11),
ring=None,
feature=None,
)
)
}
40 changes: 40 additions & 0 deletions zodipy/integration_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from zodipy._integration import IntegrationConfigFactory


_RADIAL_CUTOFF = 6

integration_configs = IntegrationConfigFactory()

integration_configs.register_config(
name='default',
components={
'cloud': (_RADIAL_CUTOFF, 250),
'band1': (_RADIAL_CUTOFF, 50),
'band2': (_RADIAL_CUTOFF, 50),
'band3': (_RADIAL_CUTOFF, 50),
'ring': (2.25, 50),
'feature': (1, 50),
}
)
integration_configs.register_config(
name='high',
components={
'cloud': (_RADIAL_CUTOFF, 500),
'band1': (_RADIAL_CUTOFF, 500),
'band2': (_RADIAL_CUTOFF, 500),
'band3': (_RADIAL_CUTOFF, 500),
'ring': (2.25, 200),
'feature': (1, 200),
}
)
integration_configs.register_config(
name='fast',
components={
'cloud': (_RADIAL_CUTOFF, 25),
'band1': (_RADIAL_CUTOFF, 25),
'band2': (_RADIAL_CUTOFF, 25),
'band3': (_RADIAL_CUTOFF, 25),
'ring': (2.25, 25),
'feature': (1, 25),
}
)
10 changes: 2 additions & 8 deletions zodipy/zodi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np

from zodipy._coordinates import get_target_coordinates, change_coordinate_system
from zodipy._integration import INTEGRATION_CONFIGS
from zodipy.integration_configs import integration_configs
from zodipy.models import models
from zodipy.simulation import InstantaneousStrategy, TimeOrderedStrategy

Expand Down Expand Up @@ -45,14 +45,8 @@ def __init__(
"""

model = models.get_model(model)
integration_config = integration_configs.get_config(integration_config)

integration_config = INTEGRATION_CONFIGS.get(integration_config.lower())
if integration_config is None:
raise ValueError(
f"Config {integration_config!r} not found. Available configs "
f"are: {list(INTEGRATION_CONFIGS.keys())}"
)

observer_locations = get_target_coordinates(observer, epochs)
earth_locations = get_target_coordinates('earth', epochs)

Expand Down

0 comments on commit 943dd14

Please sign in to comment.