<jupyter_start><jupyter_text>ClickFarm----When you don't have the data, you go to the ClickFarm!  The 10 Commandments.---1. The code is fragile. Handle with care.2. This is the master code. Don't touch this.3. **Make a copy** of this notebook and work with the copy.4. When ClickFarm Starts, you see a random image, **press skip** to start with the CT slices!5. Double clicking on the image enlarges it.6. Press Save when:    - the predicted mask is correct.    - the predicted mask is close to the bleeding but not overlapping.    - there is even a slight overlap between the bleed and the prediction.    - the prediction is only a few pixels in size but nevertheless identifies the location of the bleeding.7. Press Skip when:    - The predicted mask is empty.    - The predicted mask is nowhere near the bleeding.8. The ClickFarm saves your work, and also allows you to restore your 'save state' to continue where you left off.9. This restoration is done manually. All you need to do is pass the `Last Id` you get when you press  Save & Exit button as input to the ClickFarm object instantiation (variable pid, line 79) and your work is restored. 10. When you're done or are bored of Clicking, please press **Save & Exit**. Your clicks are not stored otherwise!<jupyter_code>%matplotlib inline
import os
import matplotlib.pyplot as plt
import numpy as np
import time
from ipywidgets import *
from IPython.display import display,clear_output
from IPython.html import widgets

import datetime
import pickle
import glob

import skimage.io as sio
path_to_img='../data/'
images = [np.random.randn(400).reshape(20,20) for i in range(10)]
images2 = [np.random.randn(400).reshape(20,20) for i in range(10)]
save_btn = widgets.Button(description="Save")
skip_btn   = widgets.Button(description="Skip")
stop_btn = widgets.Button(description="Save & Exit")

out = Output()

class ClickFarm:
    def __init__(self, patient = None, usr1=True,extn='.png'):
        
        all_imgs=sorted(glob.glob(path_to_img+'*'+extn))
        
        if usr1:
            all_imgs=all_imgs[:len(all_imgs)//2]
        if not usr1:
            all_imgs=all_imgs[len(all_imgs)//2:]
            
        if patient:
            patient=path_to_img+patient+extn
            idx=all_imgs.index(patient)
            all_imgs=all_imgs[idx:]
        
        self.files=all_imgs
        self.total=len(all_imgs)
        self.save_ids=[]
        
        self.counter = 0
        
    def plot_images(self):
        
        fig = plt.figure(figsize=[9,9])
        plt.imshow(self.get_image(self.files[self.counter]),cmap='gray')
        name=self.files[self.counter].split('/')[-1].split('.')[0]
        plt.title(f'{self.counter}/{self.total}:{name}')
        plt.axis('off')
        plt.show()
        
    def get_image(self,file):
        img=sio.imread(file)
        
        img=img[:img.shape[0]//4,:img.shape[1]//4,:]
        
        return img
        
    def on_skip_click(self,b):
        with out:
            clear_output(True)
            self.plot_images()
            self.counter += 1           
            

    def on_save_click(self,b):
        with out:
            clear_output(True)
            self.plot_images()
            name=self.files[self.counter].split('/')[-1].split('.')[0]
            self.save_ids.append(name)
            self.counter += 1

                
                
    def on_stop_click(self, b):
        print('Saving Progress...')
        now = datetime.datetime.now()
        os.makedirs('./click_farm/', exist_ok=True)
        with open(f'./click_farm/click_farm_save_{str(now.date())}_{now.hour}_{now.minute}.pkl','wb') as handle:
            pickle.dump(self.save_ids, handle)
            
        name=self.files[self.counter-1].split('/')[-1].split('.')[0]    
        print(f'The Last ID you saw: {name}.')
        print('Pass Last ID as parameter to ClickFarm to continue from here! Bye!')

        

#####
# SET pid TO YOUR LAST ID TO CONTINUE YOUR SAVED WORK.
#####
pid = 'ISIC_0029087'

bh = ClickFarm(patient=pid, usr1=True, extn='.jpg')
            
            
skip_btn.on_click(bh.on_skip_click)
save_btn.on_click(bh.on_save_click)
stop_btn.on_click(bh.on_stop_click)

print('==============')
print('ClickFarm 1.0')
print('==============')

print('------------------')
display(stop_btn)
print('------------------\n\n')

display(skip_btn)
display(save_btn)

with out:
    
    plt.imshow(images[0])
    plt.show()

out
=============
import pickle
with open('click_farm/click_farm_save_2020-01-18_9_53.pkl', 'rb') as handle:
    b = pickle.load(handle)

b[-1]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13581314878892733, 'fraction_numerical': 0.016868512110726645, 'mean_word_length': 2.610460577673692, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '137858', 'license': 'MIT', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1477, 'n_words': 397, 'path': 'Clickfarm/ClickFarm.ipynb', 'repo_name': 'AbinavRavi/OODCancer', 'size': '4624'}