-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the factory method pattern to models
- Loading branch information
Showing
8 changed files
with
110 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,57 @@ | ||
from dataclasses import dataclass | ||
from typing import Iterable, Dict | ||
|
||
from zodipy._emissivity import Emissivity | ||
from zodipy import components | ||
from zodipy.components import BaseComponent, Cloud, Band, Ring, Feature | ||
|
||
|
||
class ModelFactory: | ||
"""Class that is responsible for registring and book-keeping models.""" | ||
|
||
def __init__(self): | ||
self._models = {} | ||
|
||
def register_model(self, name, components, parameters, emissivities): | ||
model = init_model(components, parameters, emissivities) | ||
self._models[name] = model | ||
|
||
def get_model(self, name): | ||
model = self._models.get(name) | ||
if model is None: | ||
raise ValueError( | ||
f'model {name} is not registered. Available models are' | ||
f'{list(self._models.keys())}' | ||
) | ||
|
||
return model | ||
|
||
|
||
@dataclass | ||
class InterplanetaryDustModel: | ||
"""Class that represents a model of the IPD. | ||
Attributes | ||
---------- | ||
components : dict | ||
Dictionary containing initialized `zodipy.components.BaseComponent` | ||
objects. | ||
emissivities : `zodipy._emissivity.Emissivity` | ||
Emissivity object. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
components: Iterable[str], | ||
parameters: Dict[str, Dict[str, float]], | ||
emissivities: Emissivity | ||
) -> None: | ||
"""Initilizes a Model object. | ||
Parameters | ||
---------- | ||
components | ||
Iterable containing component labels as strings. | ||
parameters | ||
Dictionary containing component parameters. | ||
emissivities | ||
Emissivity object. | ||
""" | ||
|
||
self.components = self._init_components(components, parameters) | ||
self.emissivities = emissivities | ||
|
||
def _init_components(self, comp_labels: Iterable, parameters: dict) -> dict: | ||
"""Initialize component dictionary.""" | ||
|
||
comps = {} | ||
for label in comp_labels: | ||
if label.startswith('cloud'): | ||
comp_type = components.Cloud | ||
elif label.startswith('band'): | ||
comp_type = components.Band | ||
elif label.startswith('ring'): | ||
comp_type = components.Ring | ||
elif label.startswith('feature'): | ||
comp_type = components.Feature | ||
|
||
comps[label] = comp_type(**parameters[label]) | ||
|
||
return comps | ||
"""Data class representing an Interplanetary dust model.""" | ||
|
||
components: Dict[str, BaseComponent] | ||
emissivities: Emissivity | ||
|
||
|
||
def init_model( | ||
components: Iterable[str], | ||
parameters: Dict[str, Dict[str, float]], | ||
emissivities: Emissivity | ||
) -> InterplanetaryDustModel: | ||
|
||
initialized_components = {} | ||
for comp in components: | ||
if comp.startswith('cloud'): | ||
comp_type = Cloud | ||
elif comp.startswith('band'): | ||
comp_type = Band | ||
elif comp.startswith('ring'): | ||
comp_type = Ring | ||
elif comp.startswith('feature'): | ||
comp_type = Feature | ||
initialized_components[comp] = comp_type(**parameters[comp]) | ||
return InterplanetaryDustModel( | ||
components=initialized_components, | ||
emissivities=emissivities | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,25 @@ | ||
from zodipy._model import ModelFactory | ||
from zodipy import emissivities | ||
from zodipy import parameters | ||
from zodipy._model import InterplanetaryDustModel | ||
|
||
|
||
PLANCK_2013 = InterplanetaryDustModel( | ||
models = ModelFactory() | ||
|
||
models.register_model( | ||
name='planck 2013', | ||
components=('cloud', 'band1', 'band2', 'band3', 'ring', 'feature'), | ||
parameters=parameters.K98, | ||
emissivities=emissivities.PLANCK_2013 | ||
) | ||
|
||
PLANCK_2015 = InterplanetaryDustModel( | ||
components=('cloud', 'band1', 'band2', 'band3'), | ||
models.register_model( | ||
name='planck 2015', | ||
components=('cloud', 'band1', 'band2', 'band3',), | ||
parameters=parameters.K98, | ||
emissivities=emissivities.PLANCK_2015 | ||
) | ||
|
||
PLANCK_2018 = InterplanetaryDustModel( | ||
components=('cloud', 'band1', 'band2', 'band3'), | ||
models.register_model( | ||
name='planck 2018', | ||
components=('cloud', 'band1', 'band2', 'band3',), | ||
parameters=parameters.K98, | ||
emissivities=emissivities.PLANCK_2018 | ||
) | ||
|
||
|
||
MODELS = { | ||
'planck 2013' : PLANCK_2013, | ||
'planck 2015' : PLANCK_2015, | ||
'planck 2018' : PLANCK_2018 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters