Esempio n. 1
0
    def _check_events(self):
        """ Check that all attributes but centroids contain consistent data.
        Put default date, event_name and orig if not provided. Check not
        repeated events (i.e. with same date and name)

        Raises:
            ValueError
        """
        num_ev = len(self.event_id)
        num_cen = len(self.centroids.id)
        if np.unique(self.event_id).size != num_ev:
            LOGGER.error("There are events with the same identifier.")
            raise ValueError

        check.check_oligatories(self.__dict__, self.vars_oblig, 'Hazard.',
                                num_ev, num_ev, num_cen)
        check.check_optionals(self.__dict__, self.vars_opt, 'Hazard.', num_ev)
        self.event_name = check.array_default(num_ev, self.event_name, \
            'Hazard.event_name', list(self.event_id))
        self.date = check.array_default(num_ev, self.date, 'Hazard.date', \
                            np.ones(self.event_id.shape, dtype=int))
        self.orig = check.array_default(num_ev, self.orig, 'Hazard.orig', \
                            np.zeros(self.event_id.shape, dtype=bool))
        if len(self._events_set()) != num_ev:
            LOGGER.error("There are events with same date and name.")
            raise ValueError
Esempio n. 2
0
    def test_check_oligatories_fail(self):
        """Wrong DummyClass definition"""
        dummy = DummyClass()
        dummy.array = np.arange(3)
        with self.assertLogs('climada.util.checker', level='ERROR') as cm:
            with self.assertRaises(ValueError): 
                check_oligatories(dummy.__dict__, dummy.vars_oblig, "DummyClass.", 
                          dummy.id.size, dummy.id.size, 2)    
        self.assertIn('Invalid DummyClass.array size: 25 != 3.', cm.output[0])

        dummy = DummyClass()
        dummy.sparse_arr = sparse.csr.csr_matrix(np.zeros((25, 1)))
        with self.assertLogs('climada.util.checker', level='ERROR') as cm:
            with self.assertRaises(ValueError): 
                check_oligatories(dummy.__dict__, dummy.vars_oblig, "DummyClass.", 
                          dummy.id.size, dummy.id.size, 2)    
        self.assertIn('Invalid DummyClass.sparse_arr column size: 2 != 1.', cm.output[0])
    def test_check_oligatories_fail(self):
        """Wrong DummyClass definition"""
        dummy = DummyClass()
        dummy.array = np.arange(3)
        with self.assertRaises(ValueError) as cm:
            u_check.check_oligatories(dummy.__dict__, dummy.vars_oblig,
                                      "DummyClass.", dummy.id.size,
                                      dummy.id.size, 2)
        self.assertIn('Invalid DummyClass.array size: 25 != 3.',
                      str(cm.exception))

        dummy = DummyClass()
        dummy.sparse_arr = sparse.csr.csr_matrix(np.zeros((25, 1)))
        with self.assertRaises(ValueError) as cm:
            u_check.check_oligatories(dummy.__dict__, dummy.vars_oblig,
                                      "DummyClass.", dummy.id.size,
                                      dummy.id.size, 2)
        self.assertIn('Invalid DummyClass.sparse_arr column size: 2 != 1.',
                      str(cm.exception))
Esempio n. 4
0
 def test_check_oligatories_pass(self):
     """Correct DummyClass definition"""
     dummy = DummyClass()
     check_oligatories(dummy.__dict__, dummy.vars_oblig, "DummyClass.",
                       dummy.id.size, dummy.id.size, 2)