magnetic_field_factory.py 3.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# -*- coding: utf-8 -*-

import numpy as np

from keepers import Loggable

from imagine.carrier_mapper import carrier_mapper

from magnetic_field import MagneticField


class MagneticFieldFactory(Loggable, object):

    def __init__(self, box_dimensions, resolution):
        self.box_dimensions = box_dimensions
        self.resolution = resolution
17 18 19
        self._parameter_defaults = self._initial_parameter_defaults
        self._variable_to_parameter_mappings = \
            self._initial_variable_to_parameter_mappings
20

21 22 23
    @property
    def magnetic_field_class(self):
        return MagneticField
24

25 26 27
    @property
    def _initial_parameter_defaults(self):
        return {}
28

29 30 31
    @property
    def _initial_variable_to_parameter_mappings(self):
        return {}
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

    @staticmethod
    def _interval(mean, sigma, n):
        return [mean-n*sigma, mean, mean+n*sigma]

    @staticmethod
    def _positive_interval(mean, sigma, n):
        return [max(0, mean-n*sigma), mean, mean+n*sigma]

    @property
    def parameter_defaults(self):
        return self._parameter_defaults

    @parameter_defaults.setter
    def parameter_defaults(self, new_defaults):
        self._parameter_defaults.update((str(k), np.float(v))
                                        for k, v in new_defaults.items()
                                        if k in self._parameter_defaults)

    @property
    def variable_to_parameter_mappings(self):
        return self._variable_to_parameter_mappings

    @variable_to_parameter_mappings.setter
    def variable_to_parameter_mappings(self, new_mapping):
        """
        The parameter-mapping must be a dictionary with
        key: parameter-name
        value: [min, mean, max]
        """
        for k, v in new_mapping.items():
            if k in self._variable_to_parameter_mappings:
                key = str(k)
                value = [np.float(v[1]), np.float(v[2]), np.float(v[3])]
                self._variable_to_parameter_mappings.update((key, value))
                self.logger.debug("Updated variable_to_parameter_mapping %s "
                                  "to %s" % (key, str(value)))

    def _map_variables_to_parameters(self, variables):
        parameter_dict = {}
        for variable_name in variables:
            if variable_name in self.variable_to_parameter_mappings:
                mapping = self.variable_to_parameter_mappings[variable_name]
                mapped_variable = carrier_mapper(variables[variable_name],
                                                 a=mapping[0],
                                                 m=mapping[1],
                                                 b=mapping[2])
            else:
                mapped_variable = np.float(variables[variable_name])
            parameter_dict[variable_name] = mapped_variable
        return parameter_dict

84 85 86 87 88
    @staticmethod
    def _create_array(self):
        raise NotImplementedError

    def generate(self, variables={}):
89 90 91 92
        mapped_variables = self._map_variables_to_parameters(variables)
        work_parameters = self.parameter_defaults.copy()
        work_parameters.update(mapped_variables)

93
        result_magnetic_field = self.magnetic_field_class(
94 95
                                          box_dimensions=self.box_dimensions,
                                          resolution=self.resolution,
96
                                          parameters=work_parameters)
97 98

        return result_magnetic_field