From fccf4f5342d4c30441ba866b759fbca272ff21d7 Mon Sep 17 00:00:00 2001 From: Theo Steininger Date: Thu, 30 Mar 2017 10:47:30 +0200 Subject: [PATCH] Fixed default parameters in magnetic field models. --- .../ensemble_likelihood.py | 12 +-- .../jf12_magnetic_field.py | 2 +- .../jf12_magnetic_field_factory.py | 10 +-- imagine/observers/hammurapy/hammurapy.py | 86 ++++++++++--------- .../hammurapy/model_mixins/galmag_mixin.py | 6 +- .../hammurapy/model_mixins/jaffe13_mixin.py | 9 +- .../hammurapy/model_mixins/jf12_mixin.py | 10 ++- .../hammurapy/observable_mixins/mixin_base.py | 6 +- 8 files changed, 73 insertions(+), 68 deletions(-) diff --git a/imagine/likelihoods/ensemble_likelihood/ensemble_likelihood.py b/imagine/likelihoods/ensemble_likelihood/ensemble_likelihood.py index d165bd4..fd40180 100644 --- a/imagine/likelihoods/ensemble_likelihood/ensemble_likelihood.py +++ b/imagine/likelihoods/ensemble_likelihood/ensemble_likelihood.py @@ -91,13 +91,5 @@ class EnsembleLikelihood(Likelihood): (result_1, result_2, result)) # result_array[i] = result # total_result = result_array.mean() - total_result = result - normalization = measured_data.dot(measured_data) - normalized_total_result = total_result / normalization - self.logger.info("Applied normalization for total result: " - "%f / %f = %f" % - (total_result, - normalization, - normalized_total_result)) - - return normalized_total_result + + return result diff --git a/imagine/magnetic_fields/jf12_magnetic_field/jf12_magnetic_field.py b/imagine/magnetic_fields/jf12_magnetic_field/jf12_magnetic_field.py index 781f6c0..0b57ab7 100644 --- a/imagine/magnetic_fields/jf12_magnetic_field/jf12_magnetic_field.py +++ b/imagine/magnetic_fields/jf12_magnetic_field/jf12_magnetic_field.py @@ -15,7 +15,7 @@ class JF12MagneticField(MagneticField): 'b51_b0_smooth', 'b51_b0_x', 'b51_Xtheta', 'b51_r0_x', 'b51_h_disk', 'b51_Bn', 'b51_Bs', 'b51_z0_halo', 'b51_b_ring', 'b51_b0_interior', - 'b51_reg_b0', 'b51_shift'] + 'b51_shift', 'B_analytic_beta'] return parameter_list def _create_field(self): diff --git a/imagine/magnetic_fields/jf12_magnetic_field/jf12_magnetic_field_factory.py b/imagine/magnetic_fields/jf12_magnetic_field/jf12_magnetic_field_factory.py index 96e6613..ef3cbe1 100644 --- a/imagine/magnetic_fields/jf12_magnetic_field/jf12_magnetic_field_factory.py +++ b/imagine/magnetic_fields/jf12_magnetic_field/jf12_magnetic_field_factory.py @@ -13,8 +13,7 @@ class JF12MagneticFieldFactory(MagneticFieldFactory): @property def _initial_parameter_defaults(self): - defaults = {'B_field_RMS_uG': 1., - 'b51_ran_b1': 10.8, + defaults = {'b51_ran_b1': 10.8, 'b51_ran_b2': 6.96, 'b51_ran_b3': 9.59, 'b51_ran_b4': 6.96, @@ -42,8 +41,8 @@ class JF12MagneticFieldFactory(MagneticFieldFactory): 'b51_z0_halo': 5.3, 'b51_b_ring': 0.1, 'b51_b0_interior': 7.63, - 'b51_reg_b0': 1., - 'b51_shift': 0.} + 'b51_shift': 0., + 'B_analytic_beta': 1.36} return defaults @property @@ -52,7 +51,6 @@ class JF12MagneticFieldFactory(MagneticFieldFactory): def _generate_variable_to_parameter_mapping_defaults(self, n): defaults = { - 'B_field_RMS_uG': self._interval(1., 0.3, n), 'b51_ran_b1': self._interval(10.8, 2.33, n), # b_1, 1210.7820 'b51_ran_b2': self._interval(6.96, 1.58, n), # b_2, 1210.7820 'b51_ran_b3': self._interval(9.59, 1.10, n), # b_3, 1210.7820 @@ -89,7 +87,7 @@ class JF12MagneticFieldFactory(MagneticFieldFactory): # r_x^c is missing, 1204.3662 'b51_r0_x': self._positive_interval(2.9, 0.1, n), # r_x, 1204.3662 # gamma is missing (1204.3662) - 'b51_reg_b0': [0, 1, 2], 'b51_shift': [-2, 0, 2], + 'B_analytic_beta': self._interval(1.36, 0.4, n), } return defaults diff --git a/imagine/observers/hammurapy/hammurapy.py b/imagine/observers/hammurapy/hammurapy.py index 292f3c8..ef41313 100644 --- a/imagine/observers/hammurapy/hammurapy.py +++ b/imagine/observers/hammurapy/hammurapy.py @@ -16,11 +16,14 @@ from imagine.magnetic_fields.magnetic_field import MagneticField class Hammurapy(Observer): def __init__(self, hammurabi_executable, conf_directory='./confs', - working_directory_base='.', nside=128): + working_directory_base='.', nside=128, + analytic_ensemble_mean=False): self.hammurabi_executable = os.path.abspath(hammurabi_executable) self.conf_directory = os.path.abspath(conf_directory) self.working_directory_base = os.path.abspath(working_directory_base) + self.analytic_ensemble_mean = bool(analytic_ensemble_mean) + self.nside = int(nside) self.last_call_log = "" @@ -30,7 +33,7 @@ class Hammurapy(Observer): dust_template_fname = os.path.join(self.conf_directory, 'IQU_dust.fits') - self.basic_parameters = {'B_ran_mem_lim': '4', + self.basic_parameters = {'B_ran_mem_lim': '6', 'obs_shell_index_numb': '1', 'total_shell_numb': '3', 'vec_size_R': '500', @@ -41,7 +44,7 @@ class Hammurapy(Observer): 'TE_nx': '400', 'TE_ny': '400', 'TE_nz': '80', - 'TE_interp': 'T', + 'TE_interp': 'F', 'do_sync_emission': 'F', 'do_rm': 'F', 'do_dm': 'F', @@ -192,44 +195,45 @@ class Hammurapy(Observer): finally: self._remove_folder(working_directory) - # compute ensemble_mean on the last node and add it to the observables - rank = dummy.comm.rank - size = dummy.comm.size - - if rank + 1 == size: - self.logger.debug("Processing ensemble mean.") - # create a temporary folder - working_directory = self._make_temp_folder() - - # create dictionary for parameter file - parameter_dict = self.basic_parameters.copy() - - # set the parameters for an analytic run - parameter_dict['use_B_analytic'] = 'T' - - self._build_parameter_dict( - parameter_dict=parameter_dict, - magnetic_field=magnetic_field, - working_directory=working_directory, - local_ensemble_index=None) - - self._write_parameter_dict(parameter_dict=parameter_dict, - working_directory=working_directory) - - # call hammurabi - self._call_hammurabi(working_directory) - - # if hammurabi failed, _add_ensemble_mean will fail - try: + if self.analytic_ensemble_mean: + # compute ensemble_mean on last node and add it to the observables + rank = dummy.comm.rank + size = dummy.comm.size + + if rank + 1 == size: + self.logger.debug("Processing ensemble mean.") + # create a temporary folder + working_directory = self._make_temp_folder() + + # create dictionary for parameter file + parameter_dict = self.basic_parameters.copy() + + # set the parameters for an analytic run + parameter_dict['use_B_analytic'] = 'T' + + self._build_parameter_dict( + parameter_dict=parameter_dict, + magnetic_field=magnetic_field, + working_directory=working_directory, + local_ensemble_index=None) + + self._write_parameter_dict(parameter_dict=parameter_dict, + working_directory=working_directory) + + # call hammurabi + self._call_hammurabi(working_directory) + + # if hammurabi failed, _add_ensemble_mean will fail + try: + self._add_ensemble_mean(observable_dict, working_directory) + except: + self.logger.critical("Hammurabi failed! Last call log:\n" + + self.last_call_log) + raise + finally: + self._remove_folder(working_directory) + else: + working_directory = None self._add_ensemble_mean(observable_dict, working_directory) - except: - self.logger.critical("Hammurabi failed! Last call log:\n" + - self.last_call_log) - raise - finally: - self._remove_folder(working_directory) - else: - working_directory = None - self._add_ensemble_mean(observable_dict, working_directory) return observable_dict diff --git a/imagine/observers/hammurapy/model_mixins/galmag_mixin.py b/imagine/observers/hammurapy/model_mixins/galmag_mixin.py index 609f4b9..04213c1 100644 --- a/imagine/observers/hammurapy/model_mixins/galmag_mixin.py +++ b/imagine/observers/hammurapy/model_mixins/galmag_mixin.py @@ -7,12 +7,14 @@ from imagine.magnetic_fields.galmag_field import GalMagField class GalMagMixin(object): def __init__(self, hammurabi_executable, conf_directory='./confs', - working_directory_base='.', nside=128): + working_directory_base='.', nside=128, + analytic_ensemble_mean=False): self.__parameter_dict = {'B_field_type': '6'} super(GalMagMixin, self).__init__(hammurabi_executable, conf_directory, working_directory_base, - nside) + nside, + analytic_ensemble_mean) @property def magnetic_field_class(self): diff --git a/imagine/observers/hammurapy/model_mixins/jaffe13_mixin.py b/imagine/observers/hammurapy/model_mixins/jaffe13_mixin.py index a754593..92e3637 100644 --- a/imagine/observers/hammurapy/model_mixins/jaffe13_mixin.py +++ b/imagine/observers/hammurapy/model_mixins/jaffe13_mixin.py @@ -5,11 +5,13 @@ from imagine.magnetic_fields.jaffe13_magnetic_field import Jaffe13MagneticField class Jaffe13Mixin(object): def __init__(self, hammurabi_executable, conf_directory='./confs', - working_directory_base='.', nside=128): + working_directory_base='.', nside=128, + analytic_ensemble_mean=False): self.__parameter_dict = {'B_field_type': '10', 'B_field_do_random': 'T', 'B_analytic_beta': '0', - 'B_field_RMS_uG': '3.5', + 'B_f_ord': '1.0', + 'B_field_RMS_uG': '1.0', 'bb_molr_aniso': 'T', 'bb_ord_interarm': 'T', 'bb_scale_coh_amps': 'T', @@ -19,7 +21,8 @@ class Jaffe13Mixin(object): super(Jaffe13Mixin, self).__init__(hammurabi_executable, conf_directory, working_directory_base, - nside) + nside, + analytic_ensemble_mean) @property def magnetic_field_class(self): diff --git a/imagine/observers/hammurapy/model_mixins/jf12_mixin.py b/imagine/observers/hammurapy/model_mixins/jf12_mixin.py index 6437e86..9776447 100644 --- a/imagine/observers/hammurapy/model_mixins/jf12_mixin.py +++ b/imagine/observers/hammurapy/model_mixins/jf12_mixin.py @@ -5,14 +5,18 @@ from imagine.magnetic_fields.jf12_magnetic_field import JF12MagneticField class JF12Mixin(object): def __init__(self, hammurabi_executable, conf_directory='./confs', - working_directory_base='.', nside=128): + working_directory_base='.', nside=128, + analytic_ensemble_mean=False): self.__parameter_dict = {'B_field_type': '7', 'B_field_do_random': 'T', - 'B_analytic_beta': '1.36'} + 'B_field_RMS_uG': '1.0', + 'b51_reg_b0': '1.0', + } super(JF12Mixin, self).__init__(hammurabi_executable, conf_directory, working_directory_base, - nside) + nside, + analytic_ensemble_mean) @property def magnetic_field_class(self): diff --git a/imagine/observers/hammurapy/observable_mixins/mixin_base.py b/imagine/observers/hammurapy/observable_mixins/mixin_base.py index 7d69ab9..9872541 100644 --- a/imagine/observers/hammurapy/observable_mixins/mixin_base.py +++ b/imagine/observers/hammurapy/observable_mixins/mixin_base.py @@ -15,12 +15,14 @@ from imagine.observables import Observable class MixinBase(object): def __init__(self, hammurabi_executable, conf_directory='./confs', - working_directory_base='.', nside=128): + working_directory_base='.', nside=128, + analytic_ensemble_mean=False): self.__hpSpace = HPSpace(nside=int(nside)) super(MixinBase, self).__init__(hammurabi_executable, conf_directory, working_directory_base, - nside) + nside, + analytic_ensemble_mean) @property def __config_dict(self): -- GitLab