# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
import glob
import matplotlib.pyplot as plt
# %matplotlib inline
from tqdm import tqdm

# !ls

glob.glob('*.csv')

fns = [
    '20191023_02_29_blending_unet_b3b5b7_mean_sub_lb660.csv',
    'ens_7models_tta_lb662.csv',
    'submission_segmentation_and_classifier_lb660.csv'
]

dfs = [pd.read_csv(x) for x in fns]

for df in dfs:
    df.EncodedPixels = df.EncodedPixels.fillna('')

dfs[0].head(5)

all(dfs[0].Image_Label.values == dfs[2].Image_Label.values)


# +
def rle_decode(mask_rle: str = '', shape: tuple = (350, 525)):
    '''
    Decode rle encoded mask.
    
    :param mask_rle: run-length as string formatted (start length)
    :param shape: (height, width) of array to return 
    Returns numpy array, 1 - mask, 0 - background
    '''
    if len(mask_rle) < 1:
        return np.zeros(shape, dtype=np.uint8)
    s = mask_rle.split()
    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
    starts -= 1
    ends = starts + lengths
    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)
    for lo, hi in zip(starts, ends):
        img[lo:hi] = 1
    return img.reshape(shape, order='F')

def mask2rle(img):
    '''
    Convert mask to rle.
    img: numpy array, 1 - mask, 0 - background
    Returns run length as string formated
    '''
    pixels= img.T.flatten()
    pixels = np.concatenate([[0], pixels, [0]])
    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
    runs[1::2] -= runs[::2]
    return ' '.join(str(x) for x in runs)



# -

for df in dfs:
    df['masks'] = df.EncodedPixels.map(lambda x: rle_decode(x))

dfs[0].head(10)

plt.imshow(dfs[0].iloc[7].masks)

mean_masks = []
for i in tqdm(range(len(dfs[0])), total=len(dfs[0])):
    img_masks = []
    for df in dfs:
        img_masks.append(df.iloc[i].masks)
    mean_masks.append((np.mean(img_masks, 0) > 0.5).astype(np.uint8))

dfs[0]['mean_masks'] = mean_masks

plt.imshow(dfs[0].iloc[7].mean_masks)

dfs[0].EncodedPixels = dfs[0].mean_masks.map(lambda x: mask2rle(x))

dfs[0].head(10)

dfs[0].to_csv('mean_3dfs_tmp2.csv', columns=['Image_Label', 'EncodedPixels'], index=False)

# !md5sum *.csv

# !ls -lh

# !kaggle competitions list

# !kaggle competitions submit -c understanding_cloud_organization -f mean_3dfs.csv -m 'mean df'

# !head -n 2 mean_3dfs.csv



StarcoderdataJupyterScriptsDedupFiltered