Skip to content

Unupdated type check against csr_matrix leads to a bug in Hazard.write_hdf5() #1260

@spjuhel

Description

@spjuhel

Describe the bug
Trying to write a Hazard with Hazard.write_hdf5() raises the following warning and does not write the intensity matrix:

2026-03-09 11:24:28,337 - climada.hazard.io - WARNING - write_hdf5: the class member intensity is skipped, due to its type, csr_array, for which writing to hdf5 is not implemented. Reading this H5 file will probably lead to intensity being set to its default value.

This is related to #1255, and #5a6d536

Expected behavior

Hazard.write_hdf5() should not raise a warning and correctly write the object.

Suggested way forward

  1. We can enforce a csr_matrix type here (probably preferred as this is the real type we use):

return sparse_coo.tocsr() # Convert sparse.COO to scipy.sparse.csr_matrix
to
return csr_matrix(sparse_coo.tocsr()) # Convert sparse.COO to scipy.sparse.csr_matrix

5a6d536#diff-3175d04f0013b63b2662d6ced577cceeb7d458cc2a41d4711bbfff7f8577f0cbR61

or

  1. Change the type check in the hdf5 writer here:

elif isinstance(var_val, sparse.csr_matrix):

(probably other writers too)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions