def _validate_ld_matrix(ld_mat):
    """
    Takes an `LDMatrix` object and checks its contents for validity.
    Specifically, we check that:
     - The dimensions of the matrix and its associated attributes are matching.
     - The LD boundaries are correct.
     - The masking is working properly.
    :param ld_mat: An instance of `LDMatrix`
    :return: True if `ld_mat` has the correct structure, False otherwise.
    """

    attributes = ['snps', 'a1', 'maf', 'bp_position', 'cm_position', 'ld_score']

    for attr in attributes:
        attribute = getattr(ld_mat, attr)
        if attribute is None:
            continue
        if len(attribute) != ld_mat.n_elements:
            raise ValueError("Invalid LD Matrix: Attribute dimensions are not aligned!")

    # Check LD bounds:
    ld_bounds = ld_mat.get_masked_boundaries()

    if ld_bounds.shape != (2, ld_mat.n_elements):
        raise ValueError("Invalid LD Matrix: LD boundaries have the wrong dimensions!")

    ld_block_lengths = ld_bounds[1, :] - ld_bounds[0, :]

    # Iterate over the stored LD data to check its dimensions:
    i = 0

    for i, d in enumerate(ld_mat):
        if len(d) != ld_block_lengths[i]:
            raise ValueError(f"Invalid LD Matrix: Element {i} does not have matching LD boundaries!")

    if i != (ld_mat.n_elements - 1):
        raise ValueError(f"Invalid LD Matrix: Conflicting total number of elements!")

    return True