Exemplo n.º 1
0
def test_invalid_range():
    sample = torch.Tensor([[1,2,0,7],[2,1,4,6],[3,5,1,0]])
    try:
        H,axes = histogramdd_pytorch(sample,bins = [3,4,8], range=[(1,2),None,(5,4)])
        ok = False
    except ValueError:
        ok = True
    assert ok, "Accepted invalid input: invalid range"
Exemplo n.º 2
0
def test_invalid_bins():
    sample = torch.Tensor([[1,2,0,7],[2,1,4,6],[3,5,1,0]])
    try:
        H,axes = histogramdd_pytorch(sample,bins = [3,4,0])
        ok = False
    except ValueError:
        ok = True
    assert ok, "Accepted invalid input: bins must be positive"
Exemplo n.º 3
0
def test_invalid_bins_custombinning_empty_axis():
    try:
        from torch import searchsorted
    except ImportError:
        pytest.skip("Current Pytorch version doesn't supoport searchsorted")
    sample = torch.Tensor([[1,2,0,7],[2,1,4,6],[3,5,1,0]])
    try:
        H,axes = histogramdd_pytorch(sample,bins = [torch.tensor([1,2,3]),torch.tensor([2,3,4]),torch.tensor([1])])
        ok = False
    except ValueError:
        ok = True
    assert ok, "Accepted invalid input: each axis must have at least 2 edges for custom binning"
Exemplo n.º 4
0
def test_histogram_uniform(seed,N,D,bins,device):
    if device == "cuda:0" and not torch.cuda.is_available():
        pytest.skip("CUDA is not available with pytorch")
    torch.random.manual_seed(seed)
    sample = torch.rand(D,N,device=device)
    H,axes = histogramdd_pytorch(sample,bins)
    Hnp,npaxes = np.histogramdd(sample.T.cpu().numpy(),bins)
    assert len(axes) == D, "Number of axes invalid"
    assert torch.sum(H) <= N, "Sum of histogram too high"
    assert np.max(np.abs(H.cpu().numpy() - Hnp)) <= 1, "Histogram inconsistent with numpy histogram"
    for i in range(len(axes)):
        assert len(axes[i]) == bins[i]+1, "Invalid number of edges"
Exemplo n.º 5
0
def makeHistogram(data, hisString, **kwargs):
    r"""
    Make N dimensional numpy histograms for given histogram string
    :param data:            input panda
    :param hisString:       histogram setup string
    :param kwargs           options
    :return:
        dictionary with histogram information
            * H              - ndarrray
            * axis           - axis description
            * varNames       - variable Names
            * name           - histogram name

    Example usage:
        >>> makeHistogram(MCdata,"TRD:pmeas:particle:#TRD>0>>hisTRDPP(50,0.5,3,20,0.3,5,5,0,5)",3)
    """
    options = {}
    options.update(histoNDOptions)
    options.update(kwargs)
    verbose = options['verbose']
    if verbose & 0x1:
        logging.info("makeHistogram   :%s", hisString)
    varList = hisString.split(":")
    description = varList[-1].replace('#', '')
    description = description.split(">>")
    selection = description[0]
    histoInfo = description[1]
    del (varList[-1])
    histoInfo = histoInfo.replace('(', ',').replace(')', ',').split(",")
    bins = []
    hRange = []
    for idx, a in enumerate(varList):
        bins.append(int(histoInfo[idx * 3 + 1]))
        hRange.append(
            (float(histoInfo[idx * 3 + 2]), float(histoInfo[idx * 3 + 3])))
    if verbose & 0x2:
        logging.info("Histogram Name  :%s", histoInfo[0])
        logging.info("Variable list   :%s", varList)
        logging.info("Histogram bins  :%s", bins)
        logging.info("Histogram range :%s", hRange)
    if options['use_pytorch']:
        H, axis = histogramdd_pytorch(torch.from_numpy(
            data.query(selection)[varList].values).T,
                                      bins=bins,
                                      range=hRange)
        H = H.numpy()
        axis = [i.numpy() for i in axis]
    else:
        H, axis = np.histogramdd(data.query(selection)[varList].values,
                                 bins=bins,
                                 range=hRange)
    return {"H": H, "axes": axis, "name": histoInfo[0], "varNames": varList}
Exemplo n.º 6
0
def test_histogram_uniform_randombins(seed,N,D,bins,device):
    if device == "cuda:0" and not torch.cuda.is_available():
        pytest.skip("CUDA is not available with pytorch")
    try:
        from torch import searchsorted
    except ImportError:
        pytest.skip("Current Pytorch version doesn't supoport searchsorted")
    torch.random.manual_seed(seed)
    sample = torch.rand(D,N,device=device)
    nbins = bins
    bins = [torch.sort(torch.rand(i+1,device=device))[0] for i in nbins] #+1 because there's 1 more fence post than the length of the fence
    H,axes = histogramdd_pytorch(sample,bins)
    Hnp,npaxes = np.histogramdd(sample.T.cpu().numpy(),[i.cpu().numpy() for i in bins])
    assert len(axes) == D, "Number of axes invalid"
    assert torch.sum(H) <= N, "Sum of histogram too high"
    assert np.max(np.abs(H.cpu().numpy() - Hnp)) <= 1, "Histogram inconsistent with numpy histogram"
    for i in range(len(axes)):
        assert len(axes[i]) == nbins[i]+1, "Invalid number of edges"
Exemplo n.º 7
0
def test_histogram_singular():
    H,axes = histogramdd_pytorch(torch.zeros(5,100000),9)
    assert H[4,4,4,4,4] == 100000, "Test for all zeros failed"
    assert len(axes) == 5, "Number of axes invalid"
Exemplo n.º 8
0
def test_histogram_empty():
    sample = torch.empty(5,0)
    H,axes = histogramdd_pytorch(sample,bins = [2,3,4,5,6], range=[(1,2),None,None,None,None])
    assert torch.sum(H) == 0, "Sum of empty histogram is not zero"
    assert len(axes) == 5, "Number of axes invalid"