CLIMADA-project / climada_python

Python (3.8+) version of CLIMADA
GNU General Public License v3.0
321 stars 125 forks source link

Handle the case where `Hazard.select` creates a selection where the entire fraction matrix is zero #866

Closed peanutfun closed 7 months ago

peanutfun commented 7 months ago

Throw an error or a warning in this case. This will not actually handle the underlying issue: If subselecting a fraction matrix to values that are only zero, the fraction will suddenly be considered a fraction matrix of only ones (implicitly).

import numpy as np
from scipy.sparse import csr_matrix
from climada.hazard import Hazard, Centroids

centroids = Centroids(lat=np.zeros(2), lon=np.zeros(2), region_id=np.array([0, 1]))

hazard = Hazard(
    centroids=centroids,
    event_id=np.array([1, 2]),
    intensity=csr_matrix([[1, 1], [1, 1]]),
    fraction=csr_matrix([[0, 0], [1, 1]]),
)

hazard._get_fraction().toarray()
# array([[0, 0],
#        [1, 1]])

hazard.select(reg_id=[0])._get_fraction().toarray()
# array([[0],
#        [1]])

hazard.select(event_id=[1])._get_fraction()
# Returns None, equivalent to fraction of 1 everywhere, but should be
# array([[0, 0]])

First reported in #838