import torch


def histogram_dict_to_tensor(label2freq: dict, ndim: int, boolean=False):
    """Convert the histogram dictionary to a corresponding tensor
    label2freq: a dict in the form of the e.g. of {0:1, 3:1, 1:1}. Key is the index of the active dimension and the
    value is the histogram frequency. Inactive dimension is omitted in this representation.
    ndim: the resulting dimensionality of the vector
    result:
    e.g.
    given dict of {0:1, 3:1, 1:2}, the resulting tensor is [1, 2, 0, 1]
    """
    # print(label2freq)
    vector = [0] * ndim
    for k, v in label2freq.items():
        if boolean:
            vector[int(k)-1] = 1 if v else 0
        else:
            vector[int(k)-1] = v
    return torch.tensor(vector)