{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Downloading HPA public data"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import io\nimport os\nimport requests\nimport pathlib\nimport gzip\nimport imageio\nimport pandas as pd\n\n\ndef tif_gzip_to_png(tif_path):\n    '''Function to convert .tif.gz to .png and put it in the same folder\n    Eg. for working in local work station\n    '''\n    png_path = pathlib.Path(tif_path.replace('.tif.gz','.png'))\n    tf = gzip.open(tif_path).read()\n    img = imageio.imread(tf, 'tiff')\n    imageio.imwrite(png_path, img)\n    \ndef download_and_convert_tifgzip_to_png(url, target_path):    \n    '''Function to convert .tif.gz to .png and put it in the same folder\n    Eg. in Kaggle notebook\n    '''\n    r = requests.get(url)\n    f = io.BytesIO(r.content)\n    tf = gzip.open(f).read()\n    img = imageio.imread(tf, 'tiff')\n    imageio.imwrite(target_path, img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"public_hpa_df = pd.read_csv('../input/publichpa-withcellline/kaggle_2021.csv')\ncolors = ['blue', 'red', 'green', 'yellow']\ncelllines = ['A-431', 'A549', 'EFO-21', 'HAP1', 'HEK 293', 'HUVEC TERT2', 'HaCaT', 'HeLa', 'PC-3', 'RH-30', 'RPTEC TERT1', 'SH-SY5Y', 'SK-MEL-30', 'SiHa', 'U-2 OS', 'U-251 MG', 'hTCEpi']\npublic_hpa_df_17 = public_hpa_df[public_hpa_df.Cellline.isin(celllines)]\nlen(public_hpa_df), len(public_hpa_df_17)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = public_hpa_df[public_hpa_df.Label == 'No staining'].sample(n=500, random_state=42).reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['downloaded'] = True","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.vision.all import zipfile\n\nwith zipfile.ZipFile('negative_cells.zip', 'w') as img_out:    \n    for i, row in df.iterrows():\n        try:\n            img = row.Image\n            for color in colors:\n                img_url = f'{img}_{color}.tif.gz'\n                fname = f'{os.path.basename(img)}_{color}.png'\n                r = requests.get(img_url)\n                f = io.BytesIO(r.content)\n                tf = gzip.open(f).read()\n                # im = imageio.imread(tf, 'tiff')\n                # print(type(im))\n                img_out.writestr(fname, tf)\n                # with open(save_path, 'wb') as f:\n                #     f.write(r.content)\n                # download_and_convert_tifgzip_to_png(img_url, save_path)\n                # print(f'Downloaded {img_url} as {save_path}')    \n        except:\n            df['downloaded'].loc[i] = False\n            print(f'failed to download: {img}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv('df_negative.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !unzip negative_cells.zip\n\n# import matplotlib.pyplot as plt\n# im = imageio.imread('956_D3_7_green.png', 'tiff')\n# plt.imshow(im)","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}