def generate_data_set():
    while True:
        b0 = random.randint(0, 140)
        b1 = random.randint(0, 140)
        d0 = min(b0 + random.randint(0, 127), 255)
        d1 = min(b1 + random.randint(0, 127), 255)
        label = random.choice([0, 1])
        if label == 0:
            yield np.array([b0, d0, b1, d1]), 0
        else:
            yield np.array([d0, b0, d1, b1]), 1
Exemple #2
0
def test_setitem_and_copy(tt):
    ttc = copy(tt)
    if len(ttc) > 0:
        a = ttc[0]
        b = ttc[-1]
        ttc[0] = np.array([1, 2, 3])
        ttc[-1] = np.array([1, 2, 3])
        ttc[0] = a
        ttc[-1] = b
    assert ttc == tt
    with pytest.raises(IndexError):
        ttc[len(ttc)]
def save_data_set(n: int, file_path):
    trainX, trainY = zip(*list(islice(generate_data_set(), n)))
    testX, testY = zip(*list(islice(generate_data_set(), n)))
    trainX = np.array(list(trainX))
    trainY = np.array(list(trainY))
    testX = np.array(list(testX))
    testY = np.array(list(testY))
    savemat(file_path, {
        'trainX': trainX,
        'trainY': trainY,
        'testX': testX,
        'testY': testY,
    },
            do_compression=True)
 def shrink(images):
     return np.array([
         shrink_quadratic(img, new_side_length).flatten() for img in images
     ])
from tensor_networks.transposition import transpose_outer_indices, reverse_dimensions
from tests.helpers import constant_fixture, arange_from_shape

from tensor_networks.patched_numpy import np

arr = constant_fixture(params=[
    arange_from_shape(9, 9),
    np.random.random((3, 7, 5, 3, 2)),
    np.array([]),
    arange_from_shape(8),
])


def test_transpose_bond_indices(arr):
    assert (arr == transpose_outer_indices(transpose_outer_indices(arr))).all()


def test_reverse_transpose(arr):
    assert (arr == reverse_dimensions(reverse_dimensions(arr))).all()
Exemple #6
0
def image_feature_linear(absolute_colors: Array) -> Array:
    return np.array(
        list(
            map(greyscale_feature_linear,
                map(color_abs_to_percentage, absolute_colors))))
Exemple #7
0
def greyscale_feature_linear(percentage: PartialColor) -> Array:
    """
    :param percentage: A grey value
    :return: An array of a black value and a white value with a sum of 1
    """
    return np.array([percentage, 1 - percentage])
Exemple #8
0
def greyscale_feature_sin_cos(percentage: PartialColor) -> Array:
    """
    :param percentage: A grey value
    :return: An array of a black value and a white value with a sum of 1
    """
    return np.array([cos(pi / 2 * percentage), sin(pi / 2 * percentage)])
Exemple #9
0
def test_arithmetic(tt):
    assert np.array(tt.cores) - np.array(((tt / 5) * 5).cores) == approx(0)
    assert np.array(tt.cores) - np.array(((5 * tt) / 5).cores) == approx(0)