def parts_transform(patches_directory, phase, num_parts):

    patches_all, labels_all = pickling.unpickle_from_file(patches_directory +
                                                          phase + '/' +
                                                          str(0) + '.pkl')

    for i in range(1, num_parts):
        print('Parts:', i)
        patches, labels = pickling.unpickle_from_file(patches_directory +
                                                      phase + '/' + str(0) +
                                                      '.pkl')
        patches_all = np.append(patches_all, patches, axis=0)
        labels_all = np.append(labels_all, labels, axis=0)

    patches_all = patches_all[labels_all != 0]
    labels_ALL = labels_all[labels_all != 0]
    labels_ALL = labels_ALL - 1

    patches_all = np.expand_dims(patches_all, axis=1)

    patches_all = patches_all[:, :, ::2, ::2].copy()
    patches_all = patches_all[:, :, ::2, ::2].copy()

    print(patches_all.shape, labels_ALL.shape)

    #return np.array([x_0, x_1])
    return patches_all, labels_ALL
def small_mammogram_loading(patches_directory, phase, num_parts, resize=0):

    all_views = ['L-CC', 'L-MLO', 'L-CC', 'R-MLO']
    patches_all_by_view = dict.fromkeys(all_views)
    labels_all_by_view = dict.fromkeys(all_views)

    patches, labels, view = pickling.unpickle_from_file(patches_directory +
                                                        phase + '/' + str(0) +
                                                        '.pkl')
    labels = np.array([breast_data.data.map_label(label) for label in labels])
    patches = patches.astype(np.float32)
    patches = patches.transpose(0, 2, 3, 1)

    patches_all_by_view[view[0]] = patches
    labels_all_by_view[view[0]] = labels

    for i in range(1, num_parts):
        print('Parts:', i)
        patches, labels, view = pickling.unpickle_from_file(patches_directory +
                                                            phase + '/' +
                                                            str(0) + '.pkl')
        labels = np.array(
            [breast_data.data.map_label(label) for label in labels])
        #print(labels.shape, patches.shape)
        patches = patches.astype(np.float32)
        patches = patches.transpose(0, 2, 3, 1)

        j = 0

        while i < resize:
            patches = patches[:, ::2, ::2, :].copy()
            j += 1

        patches_all_by_view[view[0]] = np.append(patches_all_by_view[view[0]],
                                                 patches,
                                                 axis=0)
        labels_all_by_view[view[0]] = np.append(labels_all_by_view[view[0]],
                                                labels,
                                                axis=0)

    result = []
    for view in all_views:
        if labels_all_by_view[view] != None:
            for label in [0, 1, 2]:
                result.append(patches_all_by_view[view][
                    labels_all_by_view[view] == label])

    result = np.array(result)

    return result
def small_mammogram_loading(patches_directory, phase, num_parts):

    patches_all, labels_all, view = pickling.unpickle_from_file(
        patches_directory + phase + '/' + str(0) + '.pkl')

    for i in range(1, num_parts):
        print('Parts:', i)
        patches, labels, view = pickling.unpickle_from_file(patches_directory +
                                                            phase + '/' +
                                                            str(0) + '.pkl')
        patches_all = np.append(patches_all, patches, axis=0)
        labels_all = np.append(labels_all, labels, axis=0)

    patches_all = patches_all.astype(np.float32)
    patches_all = patches_all[:, :, ::2, ::2].copy()
    patches_all = patches_all[:, :, ::2, ::2].copy()

    print(patches_all.shape, labels_all.shape)

    #return np.array([x_0, x_1])
    return patches_all, labels_all
def parts_combination(patches_directory, phase, num_parts):

    x, _ = pickling.unpickle_from_file(patches_directory + phase + '/' +
                                       str(0) + '.pkl')

    x_0, x_1, x_2, x_3 = x[0], x[1], x[2], x[3]

    y = []
    x = np.append(x_1, x_2, axis=0)
    x = np.append(x, x_3, axis=0)
    y.append([0] * x_1.shape[0])
    y.append([1] * x_2.shape[0])
    y.append([2] * x_3.shape[0])

    for i in range(1, num_parts):
        print('Parts:', i)
        patches, _ = pickling.unpickle_from_file(patches_directory + phase +
                                                 '/' + str(i) + '.pkl')
        if patches[0].shape[0] != 0:
            x_0 = np.append(x_0, patches[0], axis=0)

        if patches[1].shape[0] != 0:
            x = np.append(x, patches[1], axis=0)
            y.append([0] * patches[1].shape[0])
        if patches[2].shape[0] != 0:
            x = np.append(x, patches[2], axis=0)
            y.append([1] * patches[2].shape[0])
        if patches[3].shape[0] != 0:
            x = np.append(x, patches[3], axis=0)
            y.append([2] * patches[3].shape[0])

    y = np.array(y)

    x = np.expand_dims(x, axis=1)
    x = x[:, ::2, ::2, :].copy()

    #return np.array([x_0, x_1])
    return x, y
def parts_combination(patches_directory, phase, num_parts, resize=0):

    x, _ = pickling.unpickle_from_file(patches_directory + phase + '/' +
                                       str(0) + '.pkl')

    #x_0, x_1, x_2, x_3 = x[0], x[1], x[2], x[3]

    x_0, x_1 = x[0], x[1]

    for i in range(1, num_parts):
        print('Parts:', i)
        patches, _ = pickling.unpickle_from_file(patches_directory + phase +
                                                 '/' + str(i) + '.pkl')
        if patches[0].shape[0] != 0:
            x_0 = np.append(x_0, patches[0], axis=0)
        if patches[1].shape[0] != 0:
            x_1 = np.append(x_1, patches[1], axis=0)
        # if patches[2].shape[0]!=0:
        #     x_2 = np.append(x_2, patches[2], axis = 0)
        # if patches[3].shape[0]!=0:
        #     x_3 = np.append(x_3, patches[3], axis = 0)

    x_0 = np.expand_dims(x_0, axis=4)
    x_1 = np.expand_dims(x_1, axis=4)

    j = 0

    while j < resize:
        x_0 = x_0[:, ::2, ::2, :]
        x_1 = x_1[:, ::2, ::2, :]
        j += 1

    # x_2 = np.expand_dims(x_2, axis=4)
    # x_3 = np.expand_dims(x_3, axis=4)

    return np.array([x_0, x_1])