Exemplo n.º 1
0
    def make_parameter_dictionary(self):
        """Parameter dictionary for area distribution proposals.
        """
        iparams = ModelHelper.make_parameter_dictionary(self)

        for key in iparams:
            if iparams[key]["dtype"] is None:
                iparams[key]["dtype"] = "GroupBox"
                sub_obj = getattr(self.config_obj, key)
                sub_cls = load_class(sub_obj)
                try:
                    sparams = ModelHelper.make_parameter_dictionary(self, fields=sub_cls._fields, obj=sub_obj)
                    iparams[key]["value"] = sparams
                except AttributeError:
                    iparams[key]["value"] = {}
                    for i, (k1, v1) in enumerate(sub_obj.items()):
                        sub_cls1 = load_class(v1)
                        sparams1 = ModelHelper.make_parameter_dictionary(self, fields=sub_cls1._fields,
                                                                         obj=v1)
                        iparams[key]["value"][i] = sparams1

                iparams1 = iparams[key]["value"]
                for k2 in iparams1:
                    if isinstance(iparams1[k2], collections.defaultdict):
                        continue
                    if iparams1[k2]["dtype"] is None:
                        iparams1[k2]["dtype"] = "GroupBox"
                        sub_obj2 = getattr(sub_obj, k2)
                        try:
                            sub_cls2 = load_class(sub_obj2)
                        except ValueError:
                            continue
                        try:
                            sparams1 = ModelHelper.make_parameter_dictionary(self, fields=sub_cls2._fields,
                                                                             obj=sub_obj2)
                            iparams1[k2]["value"] = sparams1
                        except AttributeError:
                            iparams1[k2]["value"] = {}
                            for j, (k3, v3) in enumerate(sub_obj2.items()):
                                sub_cls3 = load_class(v3)
                                sparams2 = ModelHelper.make_parameter_dictionary(self,
                                                                                 fields=sub_cls3._fields,
                                                                                 obj=v3)
                                iparams1[k2]["value"][j] = sparams2
                        iparams[key]["value"] = iparams1

        final_params = collections.OrderedDict()
        for parameter_name in self.parameter_order:
            final_params[parameter_name] = iparams.get(parameter_name)

        return final_params
Exemplo n.º 2
0
    def make_parameter_dictionary(self):
        """Parameter dictionary for area distribution proposals.
        """
        iparams = ModelHelper.make_parameter_dictionary(self)

        for key in iparams:
            if iparams[key]["dtype"] is None:
                iparams[key]["dtype"] = "GroupBox"
                sub_obj = getattr(self.config_obj, key)
                sub_cls = load_class(sub_obj)
                try:
                    sparams = ModelHelper.make_parameter_dictionary(self, fields=sub_cls._fields, obj=sub_obj)
                    iparams[key]["value"] = sparams
                except AttributeError:
                    iparams[key]["value"] = {}
                    for i, (k1, v1) in enumerate(sub_obj.items()):
                        sub_cls1 = load_class(v1)
                        sparams1 = ModelHelper.make_parameter_dictionary(self, fields=sub_cls1._fields,
                                                                         obj=v1)
                        iparams[key]["value"][k1] = sparams1

                iparams1 = iparams[key]["value"]
                for k2 in iparams1:
                    if isinstance(iparams1[k2], collections.defaultdict):
                        continue
                    if iparams1[k2]["dtype"] is None:
                        iparams1[k2]["dtype"] = "GroupBox"
                        sub_obj2 = getattr(sub_obj, k2)
                        try:
                            sub_cls2 = load_class(sub_obj2)
                        except ValueError:
                            continue
                        try:
                            sparams1 = ModelHelper.make_parameter_dictionary(self, fields=sub_cls2._fields,
                                                                             obj=sub_obj2)
                            iparams1[k2]["value"] = sparams1
                        except AttributeError:
                            iparams1[k2]["value"] = {}
                            for j, (k3, v3) in enumerate(sub_obj2.items()):
                                sub_cls3 = load_class(v3)
                                sparams2 = ModelHelper.make_parameter_dictionary(self,
                                                                                 fields=sub_cls3._fields,
                                                                                 obj=v3)
                                iparams1[k2]["value"][j] = sparams2
                        iparams[key]["value"] = iparams1

        final_params = collections.OrderedDict()
        for parameter_name in self.parameter_order:
            final_params[parameter_name] = iparams.get(parameter_name)

        return final_params
Exemplo n.º 3
0
class ModelHelperTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.save_dir = "config_dir"
        if not os.path.exists(cls.save_dir):
            os.mkdir(cls.save_dir)

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.save_dir)

    def setUp(self):
        self.mh = ModelHelper(ObservingSite())

    def test_basic_information_after_creation(self):
        self.assertIsNotNone(self.mh.config_obj)
        self.assertIsNotNone(self.mh.config_cls)
        self.assertIsNotNone(self.mh.paren_match)
        self.assertIsNotNone(self.mh.params)

    def test_blank_helper_basic_information_after_creation(self):
        blank_mh = ModelHelper()
        self.assertIsNone(blank_mh.config_obj)
        self.assertIsNone(blank_mh.config_cls)
        self.assertIsNotNone(blank_mh.paren_match)
        self.assertIsNone(blank_mh.params)

    def test_make_parameter(self):
        # Setup top-level information for one parameter
        param_dict = collections.defaultdict(dict)
        fields = self.mh.config_cls._fields
        key = "name"
        value = fields[key]
        pdict = param_dict[key]
        self.mh.make_parameter(pdict, key, value)
        self.assertEqual(pdict["dtype"], "Str")
        self.assertEqual(pdict["value"], "Cerro Pachon")
        self.assertIsNone(pdict["units"])

    def test_make_parameter_dictionary(self):
        param_dict = self.mh.make_parameter_dictionary()
        self.assertEqual(len(param_dict), 7)

    def test_check_parameter(self):
        self.assertTrue(self.mh.check_parameter("name", "Sierra Madre"))
        self.assertFalse(self.mh.check_parameter("height", "2650.0"))

    def test_get_parameter(self):
        self.assertEquals(self.mh.get_parameter("name"), "Cerro Pachon")
        self.assertEquals(self.mh.get_parameter("height"), 2650.0)

    def test_save_configuration(self):
        name = "obs_site"
        changed_values = [("name", "Sierra Madre"), ("height", "1243.5"), ("bool_val", "False"),
                          ("str_list", "a,b,c,d"), ("float_list", "0.0,1.0,2.0")]
        self.mh.save_configuration(self.save_dir, name, changed_values)
        output_file = "{}.py".format(name)
        full_file = os.path.join(self.save_dir, output_file)
        self.assertTrue(os.path.exists(full_file))
        with open(full_file, 'r') as ifile:
            lines = ifile.readlines()
            self.assertEqual(len(lines), len(changed_values) + 2)
class ModelHelperTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.save_dir = "config_dir"
        if not os.path.exists(cls.save_dir):
            os.mkdir(cls.save_dir)

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.save_dir)

    def setUp(self):
        self.mh = ModelHelper(ObservingSite())

    def test_basic_information_after_creation(self):
        self.assertIsNotNone(self.mh.config_obj)
        self.assertIsNotNone(self.mh.config_cls)
        self.assertIsNotNone(self.mh.paren_match)
        self.assertIsNotNone(self.mh.params)

    def test_blank_helper_basic_information_after_creation(self):
        blank_mh = ModelHelper()
        self.assertIsNone(blank_mh.config_obj)
        self.assertIsNone(blank_mh.config_cls)
        self.assertIsNotNone(blank_mh.paren_match)
        self.assertIsNone(blank_mh.params)

    def test_make_parameter(self):
        # Setup top-level information for one parameter
        param_dict = collections.defaultdict(dict)
        fields = self.mh.config_cls._fields
        key = "name"
        value = fields[key]
        pdict = param_dict[key]
        self.mh.make_parameter(pdict, key, value)
        self.assertEqual(pdict["dtype"], "Str")
        self.assertEqual(pdict["value"], "Cerro Pachon")
        self.assertIsNone(pdict["units"])

    def test_make_parameter_dictionary(self):
        param_dict = self.mh.make_parameter_dictionary()
        self.assertEqual(len(param_dict), 7)

    def test_check_parameter(self):
        self.assertTrue(self.mh.check_parameter("name", "Sierra Madre"))
        self.assertFalse(self.mh.check_parameter("height", "2650.0"))

    def test_get_parameter(self):
        self.assertEquals(self.mh.get_parameter("name"), "Cerro Pachon")
        self.assertEquals(self.mh.get_parameter("height"), 2650.0)

    def test_save_configuration(self):
        name = "obs_site"
        changed_values = [
            ("name", "Sierra Madre"),
            ("height", "1243.5"),
            ("bool_val", "False"),
            ("str_list", "a,b,c,d"),
            ("float_list", "0.0,1.0,2.0"),
        ]
        self.mh.save_configuration(self.save_dir, name, changed_values)
        output_file = "{}.py".format(name)
        full_file = os.path.join(self.save_dir, output_file)
        self.assertTrue(os.path.exists(full_file))
        with open(full_file, "r") as ifile:
            lines = ifile.readlines()
            self.assertEqual(len(lines), len(changed_values) + 2)
Exemplo n.º 5
0
    def make_parameter_dictionary(self):
        """Parameter dictionary for sequence proposals.
        """
        iparams = ModelHelper.make_parameter_dictionary(self)

        for key in iparams:
            if iparams[key]["dtype"] is None:
                iparams[key]["dtype"] = "GroupBox"
                sub_obj = getattr(self.config_obj, key)
                sub_cls = load_class(sub_obj)
                try:
                    sparams = ModelHelper.make_parameter_dictionary(
                        self, fields=sub_cls._fields, obj=sub_obj)
                    iparams[key]["value"] = sparams
                except AttributeError:
                    iparams[key]["value"] = {}
                    for i, (k1, v1) in enumerate(sub_obj.items()):
                        sub_cls1 = load_class(v1)
                        sparams1 = ModelHelper.make_parameter_dictionary(
                            self, fields=sub_cls1._fields, obj=v1)
                        iparams[key]["value"][k1] = sparams1
                        if "master" in key:
                            iparams[key]["value"][k1]["sub_sequences"][
                                "value"] = {}
                            # Need to handle sub-sequence lists
                            sub_seq_names = []
                            for k3, v3 in v1.sub_sequences.items():
                                sub_cls3 = load_class(v3)
                                sparams3 = ModelHelper.make_parameter_dictionary(
                                    self, fields=sub_cls3._fields, obj=v3)
                                iparams[key]["value"][k1]["sub_sequences"][
                                    "value"][k3] = sparams3
                                sub_seq_names.append(sparams3["name"]["value"])
                            iparams[key]["value"][k1]["sub_sequence_names"] = \
                                {"units": None, "dtype": "StringList", "format": None,
                                 "doc": iparams[key]["value"][0]["sub_sequences"]["doc"],
                                 "value": ','.join(sub_seq_names)}

                iparams1 = iparams[key]["value"]
                for k2 in iparams1:
                    if isinstance(iparams1[k2], collections.defaultdict):
                        continue
                    if iparams1[k2]["dtype"] is None:
                        iparams1[k2]["dtype"] = "GroupBox"
                        sub_obj2 = getattr(sub_obj, k2)
                        try:
                            sub_cls2 = load_class(sub_obj2)
                        except ValueError:
                            continue
                        try:
                            sparams1 = ModelHelper.make_parameter_dictionary(
                                self, fields=sub_cls2._fields, obj=sub_obj2)
                            iparams1[k2]["value"] = sparams1
                        except AttributeError:
                            iparams1[k2]["value"] = {}
                            for j, (k3, v3) in enumerate(sub_obj2.items()):
                                sub_cls3 = load_class(v3)
                                sparams2 = ModelHelper.make_parameter_dictionary(
                                    self, fields=sub_cls3._fields, obj=v3)
                                iparams1[k2]["value"][j] = sparams2
                        iparams[key]["value"] = iparams1

        final_params = collections.OrderedDict()
        for parameter_name in self.parameter_order:
            final_params[parameter_name] = iparams.get(parameter_name)

        return final_params