{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pickle\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport os\nimport cuml\nfrom matplotlib import pyplot as plt\n\nopj = os.path.join","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('../input/bestfitting-ml/feats.pickle', 'rb') as handle:\n    feats = pickle.load(handle)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('../input/bestfitting-ml-public/feats_ext.pickle', 'rb') as handle:\n    feats_ext = pickle.load(handle)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"feats.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"feats_ext.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/bestfitting-ml/feats_df.csv')\ndf['dataset'] = 'train'\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_ext = pd.read_csv('../input/bestfitting-ml-public/feats_df_ext.csv')\ndf_ext['dataset'] = 'public'\ndf_ext.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"all_feats = np.vstack([feats, feats_ext])\nall_feats.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_rgby(img_id,train=True):\n    if train: img_dir = '../input/hpa-single-cell-image-classification/train'\n    else: img_dir = '../input/hpa-public-768-excl-0-16/hpa_public_excl_0_16_768/small'\n    suffix = '.png'\n    colors = ['red', 'green', 'blue'] #, 'yellow']\n    flags = cv2.IMREAD_GRAYSCALE\n    img = [cv2.imread(opj(img_dir, img_id + '_' + color + suffix), flags)\n           for color in colors]\n    img = np.stack(img, axis=-1)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.concat([df,df_ext], ignore_index=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"umap = cuml.UMAP(n_neighbors=2, n_components=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ump = umap.fit_transform(all_feats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ump.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"all_feats.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Compute DBSCAN\ndb = cuml.cluster.DBSCAN(eps=0.05, min_samples=2).fit(ump)\ncore_samples_mask = np.zeros_like(db.labels_, dtype=bool)\ncore_samples_mask[db.core_sample_indices_] = True\nlabels = db.labels_\n\n# Number of clusters in labels, ignoring noise if present.\nn_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)\nn_noise_ = list(labels).count(-1)\n\nprint('Estimated number of clusters: %d' % n_clusters_)\nprint('Estimated number of noise points: %d' % n_noise_)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"clusters = set(list(labels))\n\nd = {}\nfor c in clusters:\n    d[c] = 0\nfor l in labels:\n    d[l] += 1\nd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f = [i for i,x in enumerate(list(labels)) if x == 5505]\n\nfor k in f:\n    \n    train = True\n    if k > len(feats): train=False\n    \n    img_id = df.ID.loc[k]\n    img_lbl = df.Label.loc[k]\n    \n    plt.figure(figsize=(10,10))\n    \n    plt.subplot(1,1,1)\n    img = read_rgby(img_id, train)\n    plt.imshow(img)\n    plt.title(f'Image: {img_id}, label: {img_lbl}')\n    \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['cluster'] = labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = df[['ID', 'Label', 'cluster', 'dataset']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv('combined.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f = [48868, 48869]\nfor k in f:\n    \n    train = True\n    if k > len(feats): train=False\n    \n    img_id = df.ID.loc[k]\n    img_lbl = df.Label.loc[k]\n    \n    plt.figure(figsize=(10,10))\n    \n    plt.subplot(1,1,1)\n    img = read_rgby(img_id, train)\n    plt.imshow(img)\n    plt.title(f'Image: {img_id}, label: {img_lbl}')\n    \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}