English
File size: 3,692 Bytes
26225c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import torch
import ipywidgets as widgets
from ipyfilechooser import FileChooser
from IPython.display import display
from src.utils.configs import get_config_structure


__all__ = [
    'make_experiment_widgets', 'make_device_widget', 'make_split_widget',
    'make_checkpoint_file_search_widget']


def make_experiment_widgets():
    """
    Generate two co-dependent ipywidgets for selecting the task and 
    experiment from a predefined set of experiment configs.
    """
    # Parse list of experiment configs
    experiment_configs = {
        k: sorted(v[1])
        for k, v in get_config_structure()[0]['experiment'][0].items()}
    default_task = list(experiment_configs.keys())[0]
    default_expe = experiment_configs[default_task][0]

    w_task = widgets.ToggleButtons(
        options=experiment_configs.keys(),
        value=default_task,
        description="πŸ‘‰ Choose a segmentation task:",
        disabled=False,
        button_style='')

    w_expe = widgets.ToggleButtons(
        options=experiment_configs[default_task],
        value=default_expe,
        description="πŸ‘‰ Choose an experiment:",
        disabled=False,
        button_style='')

    # Define a function that updates the content of one widget based on 
    # what we selected for the other
    def update(*args):
        print(f"selected : {w_task.value}")
        w_expe.options = experiment_configs[w_task.value]
        
    w_task.observe(update)

    display(w_task)
    display(w_expe)
    
    return w_task, w_expe


def make_device_widget():
    """
    Generate an ipywidget for selecting the device on which to work
    """
    devices = [torch.device('cpu')] + [
        torch.device('cuda', i) for i in range(torch.cuda.device_count())]
    
    w = widgets.ToggleButtons(
        options=devices,
        value=devices[0],
        description="πŸ‘‰ Choose a device:",
        disabled=False,
        button_style='')
    
    display(w)
    
    return w


def make_split_widget():
    """
    Generate an ipywidget for selecting the data split on which to work
    """    
    w = widgets.ToggleButtons(
        options=['train', 'val', 'test'],
        value='val',
        description="πŸ‘‰ Choose a data split:",
        disabled=False,
        button_style='')
    
    display(w)
    
    return w


def make_checkpoint_file_search_widget():
    """
    Generate an ipywidget for locally browsing a checkpoint file
    """
    # Create and display a FileChooser widget
    w = FileChooser('', layout = widgets.Layout(width='80%'))
    display(w)
    
    # Change defaults and reset the dialog
    w.default_path = '..'
    w.default_filename = ''
    w.reset()
    
    # Shorthand reset
    w.reset(path='..', filename='')
    
    # Restrict navigation to /Users
    w.sandbox_path = '/'
    
    # Change hidden files
    w.show_hidden = False
    
    # Customize dir icon
    w.dir_icon = '/'
    w.dir_icon_append = True
    
    # Switch to folder-only mode
    w.show_only_dirs = False
    
    # Set a file filter pattern (uses https://docs.python.org/3/library/fnmatch.html)
    # w.filter_pattern = '*.txt'
    w.filter_pattern = '*.ckpt'
    
    # Set multiple file filter patterns (uses https://docs.python.org/3/library/fnmatch.html)
    # w.filter_pattern = ['*.jpg', '*.png']
    
    # Change the title (use '' to hide)
    w.title = "πŸ‘‰ Choose a checkpoint file *.ckpt relevant to your experiment (eg use our or your own pretrained models for this):"
    
    # Sample callback function
    def change_title(chooser):
        chooser.title = 'Selected checkpoint:'
    
    # Register callback function
    w.register_callback(change_title)

    return w