def __init__(self, data, model=None, all_models=None, axes_style={},
                data_style={}, init_style={}, best_style={}, **kwargs):
        # Dropdown menu of all subclasses of Model, incl. user-defined.
        self.models_menu = Dropdown()
        # Dropbox API is very different between IPy 2.x and 3.x.
        if IPY2:
            if all_models is None:
                all_models = dict([(m.__name__, m) for m in Model.__subclasses__()])
            self.models_menu.values = all_models
        else:
            if all_models is None:
                all_models = [(m.__name__, m) for m in Model.__subclasses__()]
            self.models_menu.options = all_models
        self.models_menu.on_trait_change(self._on_model_value_change,
                                             'value')
        # Button to trigger fitting.
        self.fit_button = Button(description='Fit')
        self.fit_button.on_click(self._on_fit_button_click)

        # Button to trigger guessing.
        self.guess_button = Button(description='Auto-Guess')
        self.guess_button.on_click(self._on_guess_button_click)

        # Parameter widgets are not built here. They are (re-)built when
        # the model is (re-)set.
        super(NotebookFitter, self).__init__(data, model, axes_style,
                                             data_style, init_style,
                                             best_style, **kwargs)
 def make_plot(self, b):
     controller = self.controller
     b.description = 'Creating plot... Please Wait.'
     b.disabled = True
     try:
         b.pvals = controller.pvals.copy()
         if b.plot_type.value == 'Design Space (interactive)':
             self.make_interactive_plot(b)
         elif b.plot_type.value == 'Design Space':
             self.make_static_plot(b)
         elif b.plot_type.value == 'Stability':
             self.make_stability_plot(b)
         elif b.plot_type.value == 'Eigenvalues':
             self.make_eigenvalue_plot(b)
         else:
             self.make_function_plot(b)
     except Exception as e:
         close_button = Button(description="Close")
         error_message = '<div width="200px" style="float:center; text-align:center;">'
         error_message += '<b>An error occured while plotting</b></div>'
         error_window = Popup(children=[HTML(value=error_message),close_button])
         close_button.window = error_window
         close_button.on_click(lambda x: x.window.close())
         if old_ipython is False:
             error_window.box_style='danger'
             close_button.float = 'center'
             error_window.width='250px'
             error_window.height='150px'
         display(error_window)
         b.description = 'Add Plot'
         b.disabled = False
     else:
         b.description = 'Add Plot'
         b.disabled = False
 def update_log_gains(self):
     controller = self.controller 
     case = self.case
     if case.ssystem.solution is None:
         self.log_gains.children = []
         return
     table = HTML()
     html_str = '<div><table>\n<caption>Logarithmic gains and parameter sensitivities for Case ' + case.case_number + ' (' + case.signature + '). </caption>\n'
     html_str += '<tr ><th rowspan="2" align=center  style="padding:0 15px 0 15px;"> Dependent<br> Variables </th>'
     html_str += '<th colspan="' + str(len(case.independent_variables)) + '" align=center  style="padding:0 15px 0 15px;"> Independent Variables and Parameters</th></tr><tr align=center>'
     for xi in case.independent_variables:
             html_str += '<td><b>{0}</b></td>'.format(xi)
     html_str += '</tr>\n'
     for xd in case.dependent_variables:
         html_str += '<tr><td align=center  style="padding:0 15px 0 15px;"><b>{0}</b></td>'.format(xd)
         for xi in case.independent_variables:
             html_str += '<td align=center  style="padding:0 15px 0 15px;">{0}</td>'.format(str(case.ssystem.log_gain(xd, xi)))
         html_str += '</tr>\n'
     html_str += '</table></div><br>'
     save_button = Button(description='Save Log-Gain Table')
     save_button.table_data = html_str
     save_button.on_click(self.save_table)
     table.value = html_str
     self.log_gains.children = [HTML(value='<br>'),
                                save_button, 
                                table]
     return
 def update_global_tolerances(self):
     controller = self.controller 
     case = self.case
     pvals = self.pvals
     if pvals is None:
         self.tolerances_table.children = []
         return
     if case.is_valid(p_bounds=pvals) is False:
         self.tolerances_table.children = []
         return            
     table = HTML()
     html_str = '<div><table>\n<caption>Global tolerances determined for Case ' + case.case_number + ' (' + case.signature + ') showing fold-difference to a large qualitative change{0}. </caption>\n'.format(' in log-coordinates' if self.log_coordinates is True else '') 
     html_str += '<tr ><th align=center  rowspan=2 style="padding:0 15px 0 15px;"> Parameters </th><th colspan=2> Tolerance </th></tr>'
     html_str += '<tr><td style="padding:0 15px 0 15px;"><b> Lower bound</b></td><td style="padding:0 15px 0 15px;"><b> Upper bound</b></td></tr>'
     tolerances = case.measure_tolerance(pvals, log_out=self.log_coordinates)
     for xi in sorted(pvals.keys()):
         lower_th = 1e-15 if self.log_coordinates is False else -15
         upper_th = 1e15 if self.log_coordinates is False else 15
         lower, upper = tolerances[xi]
         html_str += '<tr><td style="padding:0 15px 0 15px;"><b>{0}</b></td><td style="padding:0 15px 0 15px;">{1}</td><td style="padding:0 15px 0 15px;">{2}</td></tr>'.format(
                      xi,
                      lower if lower > lower_th else '-&infin;',
                      upper if upper < upper_th else '&infin;')
     html_str += '</table><caption>'
     html_str += 'Note: Global tolerance calculated based on the following values for the parameters: ' 
     html_str += '; '.join([i + ' = ' + str(pvals[i]) for i in sorted(pvals.keys())]) + '.'
     html_str += '</caption></div>'
     table.value = html_str
     save_button = Button(description='Save Global Tolerance Table')
     save_button.table_data = html_str
     save_button.on_click(self.save_table)
     self.tolerances_table.children = [HTML(value='<br>'),
                                       save_button,
                                       table]
     return
 def update_bounding_box(self):
     controller = self.controller 
     case = self.case
     pvals = self.pvals
     if case.is_valid(p_bounds=pvals) is False:
         self.bounding_box_table.children = []
         return
     if case.is_cyclical is True:
         self.bounding_box_table.children = []
         return            
     table = HTML()
     html_str = '<div><table>\n<caption>Bounding box determined for Case ' + case.case_number + ' (' + case.signature + '){0}. </caption>\n'.format(' in log-coordinates' if self.log_coordinates is True else '') 
     html_str += '<tr ><th align=center  rowspan=2 style="padding:0 15px 0 15px;"> Parameters </th><th colspan=2> Tolerance </th></tr>'
     html_str += '<tr><td style="padding:0 15px 0 15px;"><b> Lower bound</b></td><td style="padding:0 15px 0 15px;"><b> Upper bound</b></td></tr>'
     tolerances = case.bounding_box(log_out=self.log_coordinates)
     for xi in sorted(tolerances.keys()):
         lower_th = 1e-15 if self.log_coordinates is False else -15
         upper_th = 1e15 if self.log_coordinates is False else 15
         lower, upper = tolerances[xi]
         html_str += '<tr><td style="padding:0 15px 0 15px;"><b>{0}</b></td><td style="padding:0 15px 0 15px;">{1}</td><td style="padding:0 15px 0 15px;">{2}</td></tr>'.format(
                      xi,
                      lower if lower > lower_th else '-&infin;',
                      upper if upper < upper_th else '&infin;')
     html_str += '</table></div>'
     table.value = html_str
     save_button = Button(description='Save Bounding Box Table')
     save_button.table_data = html_str
     save_button.on_click(self.save_table)
     self.bounding_box_table.children = [HTML(value='<br>'),
                                         save_button,
                                         table]
     return
 def save_figure_widget(self, image_data, title='', caption = '', pvals=None, colors=None):
     image_widget = Image()
     image_widget.value = image_data
     children = [i for i in self.figures_widget.children]      
     html_str = '<b>Figure '+str(len(children)+1)+'.  '+title+'</b>' + caption
     html_widget = HTML(value=html_str)
     restore_pvals = Button(description='Restore Parameter Values')
     restore_pvals.pvals = pvals
     if pvals is None:
         restore_pvals.visible = False
     if old_ipython is True:
         image_widget = Image()
         image_widget.value = image_data
         tab_widget = VBox(children=[image_widget, html_widget])
         if colors is not None:
             html_widgets = self.colorbar_tabs(colors)
             tab_widget.description='Figure'
             tab_widget = Tab(children=[tab_widget]+html_widgets)
     else:
         image_widget = self._image_widget_new(image_data, colors=colors)
         tab_widget = VBox(children=[image_widget, html_widget])
     restore_pvals.on_click(self.restore_figure_pvals)
     if old_ipython is True:
         wi = Popup(children=[tab_widget, restore_pvals])
     else:
         contents = VBox(children=[tab_widget, restore_pvals])
         contents.width = '100%'
         wi = Popup(children=[contents])
         wi.border = '1px solid black'
     children.append(wi)
     self.figures_widget.children = children
     if colors is not None:
         if old_ipython is True:
             tab_widget.set_title(0, 'Figure')
             tab_widget.set_title(1, 'Colorbar')
 def make_interactive_plot(self, b):
     controller = self.controller
     if str(b.ylabel.value) == 'None':
         return
     button = Button(description='Stop interactive plot')
     button.on_click(self.remove_plot)
     button.name = 'Interactive Plot (' + str(np.random.randint(0, 1000)) + ')'
     image_widget = Image()
     popup_widget = Popup(children=[image_widget])
     image_widget.width='100%'
     rangex, rangey = self.axes_ranges(b)
     interactive_plot = controller.ds.draw_2D_slice_notebook(controller.pvals, str(b.xlabel.value),
                                                             str(b.ylabel.value),
                                                             rangex, rangey,
                                                             {i:[1e-5, 1e5] for i in
                                                              controller.pvals},
                                                             intersections=[1],
                                                             image_container=image_widget)
     wi = VBox(description=button.name,
                                  children=[interactive_plot, 
                                            button,
                                            popup_widget])
     controller.options.update({'xaxis':str(b.xlabel.value),
                                'yaxis':str(b.ylabel.value),
                                'x_range':rangex, 
                                'y_range':rangey})
     controller.update_child(button.name, wi)
Esempio n. 8
0
    def __init__(self,
                 data,
                 model=None,
                 all_models=None,
                 axes_style={},
                 data_style={},
                 init_style={},
                 best_style={},
                 **kwargs):
        # Dropdown menu of all subclasses of Model, incl. user-defined.
        self.models_menu = Dropdown()
        # Dropbox API is very different between IPy 2.x and 3.x.
        if IPY2:
            if all_models is None:
                all_models = {m.__name__: m for m in Model.__subclasses__()}
            self.models_menu.values = all_models
        else:
            if all_models is None:
                all_models = [(m.__name__, m) for m in Model.__subclasses__()]
            self.models_menu.options = all_models
        self.models_menu.on_trait_change(self._on_model_value_change, 'value')
        # Button to trigger fitting.
        self.fit_button = Button(description='Fit')
        self.fit_button.on_click(self._on_fit_button_click)

        # Button to trigger guessing.
        self.guess_button = Button(description='Auto-Guess')
        self.guess_button.on_click(self._on_guess_button_click)

        # Parameter widgets are not built here. They are (re-)built when
        # the model is (re-)set.
        super(NotebookFitter,
              self).__init__(data, model, axes_style, data_style, init_style,
                             best_style, **kwargs)
Esempio n. 9
0
    def interactive_lavalamp_pooled_inconsistent(
        self,
        sample_subsets=None,
        feature_subsets=None,
        difference_threshold=(0.001, 1.00),
        colors=["red", "green", "blue", "purple", "yellow"],
        savefile="",
    ):
        from IPython.html.widgets import interact

        savefile = "figures/last.lavalamp_pooled_inconsistent.pdf"

        def do_interact(
            sample_subset=self.default_sample_subsets,
            feature_subset=self.default_feature_subsets,
            difference_threshold=0.1,
            color=red,
            savefile=savefile,
        ):

            for k, v in locals().iteritems():
                if k == "self":
                    continue
                sys.stdout.write("{} : {}\n".format(k, v))

            assert feature_subset in self.splicing.feature_subsets.keys()
            feature_ids = self.splicing.feature_subsets[feature_subset]
            sample_ids = self.sample_subset_to_sample_ids(sample_subset)

            color = str_to_color[color]

            self.splicing.plot_lavalamp_pooled_inconsistent(sample_ids, feature_ids, difference_threshold, color=color)
            plt.tight_layout()

        if feature_subsets is None:
            feature_subsets = Interactive.get_feature_subsets(self, ["splicing", "expression"])
        if sample_subsets is None:
            sample_subsets = self.default_sample_subsets

        gui = interact(
            do_interact,
            sample_subset=sample_subsets,
            feature_subset=feature_subsets,
            difference_threshold=difference_threshold,
            color=colors,
            savefile="",
        )

        def save(w):
            filename, extension = os.path.splitext(savefile.value)
            self.maybe_make_directory(savefile.value)
            gui.widget.result.savefig(savefile.value, format=extension.lstrip("."))

        savefile = TextWidget(description="savefile", value="figures/clustermap.pdf")
        save_widget = ButtonWidget(description="save")
        gui.widget.children = list(gui.widget.children) + [savefile, save_widget]
        save_widget.on_click(save)
 def update_constraints(self):
     constraints_widget = Textarea(description='Constraints',
                                                 value = ',\n'.join(self.constraints)
                                                 )
     constraints_widget.visible = self.active_constraints
     button = Button(description='Done' if self.active_constraints else 'Modify constraints')
     button.constraints_widget = constraints_widget
     button.on_click(self.modify_constraints)
     self.constraints_widget.children = [constraints_widget, button]
 def edit_parameters_widget(self):
     controller = self.controller
     if controller.ds is None:
         return
     pvals_widgets = [FloatText(description=i, value=controller.pvals[i]) for i in sorted(controller.pvals.keys())]
     wi = VBox(children=pvals_widgets)
     button = Button(value=False, description="Done")
     button.on_click(self.update_parameters)
     button.pvals = pvals_widgets
     button.wi = wi
     edit_pvals = VBox(description="Edit Parameters", children=[wi, button])
     controller.update_child("Edit Parameters", edit_pvals)
 def edit_symbols_widget(self):
     controller = self.controller 
     if controller.ds is None:
         return
     symbols = {i:controller.symbols[i] for i in controller.symbols if i in controller.ds.dependent_variables + controller.ds.independent_variables}
     symbols.update({i:i for i in controller.ds.dependent_variables + controller.ds.independent_variables if i not in symbols})
     self.symbols = symbols
     symbols_widgets = [Text(description=i, value=self.symbols[i]) for i in symbols]
     wi = VBox(children=symbols_widgets)
     button = Button(value=False, description='Done')
     button.on_click(self.update_symbols)
     button.symbols = symbols_widgets
     button.wi = wi
     edit_symbols = VBox(description='Edit Symbols', children=[wi, button])
     controller.update_child('Edit Symbols', edit_symbols)
     wi.visible = True
    def make_display_widget(self):
        controller = self.controller 
        if controller.ds is None:
            return
        self.info = VBox()
        self.constraints_widget = VBox()
        self.plot = HBox()
        self.log_coordinates = True
        self.global_tolerance = VBox()
        close_button = Button(description='Close Tab')
        close_button.on_click(self.close_widget)
        ss_options = ['log('+ i + ')' for i in controller.ds.dependent_variables]
        self.y_dropdown = Dropdown(description='y-axis',
                                   values=ss_options,
                                   options=ss_options,
                                   value=self.y_variable)
        self.make_plot = Button(description='Create Plot')
        self.make_plot.on_click(self.change_y_axis)
        self.make_plot.yaxis = self.y_dropdown
        self.make_plot.visible = True
        check_box = Checkbox(description='Logarithmic coordinates', 
                                           value=self.log_coordinates)
        check_box.on_trait_change(self.update_log, 'value')
        self.y_dropdown.visible = False
        if len(self.slice_variables) <= 3:
            ## self.make_plot.visible = True
            if len(self.slice_variables) == 1:
                self.y_dropdown.visible = True
            if len(self.slice_variables) == 3:
                self.y_dropdown = HTML(value='<font color="red">Warning 3D plots are experimental.</font>')
        wi = VBox(children=[self.info, 
                            self.constraints_widget,
                            check_box,
                            self.y_dropdown,
                            self.make_plot,
                            self.global_tolerance,
                            close_button])
        if old_ipython is True:
            wi.set_css('height', '400px')
        else:
            wi.height='400px'
            wi.overflow_x = 'auto'
            wi.overflow_y = 'auto'

        self.update_display()
        controller.update_child(self.name, wi)
 def update_parameter_table(self):
     controller = self.controller 
     case = self.case
     pvals = self.pvals
     if pvals is None:
         self.parameter_table.children = []
         return
     if case.is_valid(p_bounds=pvals) is False:
         self.parameter_table.children = []
         return
     make_nominal = Button(description='Make Nominal Parameter Set')
     make_nominal.on_click(self.save_parameters)
     table = HTML()
     html_str = '<div><table>\n<caption>Value for the parameters automatically determined for Case ' + case.case_number + ' (' + case.signature + '). </caption>\n'
     html_str += '<tr ><th align=center  style="padding:0 15px 0 15px;"> Parameters </th><th> Value </th>'
     for xi in sorted(pvals.keys()):
             html_str += '<tr><td><b>{0}</b></td><td>{1}</td></tr>'.format(
                          xi,
                          pvals[xi])
     html_str += '</table></div>'
     table.value = html_str
     save_button = Button(description='Save Parameter Table')
     save_button.table_data = html_str
     save_button.on_click(self.save_table)
     self.parameter_table.children = [HTML(value='<br>'),
                                      save_button,
                                      table,
                                      make_nominal
                                      ]
     return
 def make_options_menu(self, b):
     wi = b.wi
     b.visible = False
     b.name.visible = False
     b.load.visible = False
     actions = [('Enumerate Phenotypes', self.enumerate_phenotypes_menu),
                ('Analyze Case', self.case_report_menu),
                ('Intersect Phenotypes', self.case_intersect_menu),
                ('Co-localize Phenotypes', self.co_localize_menu),
                ('Create Plot', self.create_plot_menu)]
     actions_h = HTML(value='<b>Actions</b>')
     options_h = HTML(value='<b>Options</b>')
     options = [('Edit Symbols', self.create_edit_symbols),
                ('Edit Parameters', self.create_edit_parameters),
                ('Save Data', self.save_widget_data)]
     actions_w = Tab()
     options_w = []
     for name, method in options:
         button = Button(description=name)
         button.on_click(method)
         ## button.version = b.version
         if method is None:
             button.disabled = True
         options_w.append(button)
     edit = Button(description='Modify System')
     edit.on_click(self.modify_equations_widget)
     warning = HTML()
     warning.value = '<font color="red"><b>Warning! Modifying system erases saved figures and tables.</b></font>'
     wi.children = [actions_h, actions_w] + [options_h] + options_w + [edit, warning]
     for title, method in actions:
         title, widget = method(self)
         children = [i for i in actions_w.children] + [widget]
         actions_w.children = children
         actions_w.set_title(len(children)-1, title)
     self.widget.selected_index = 1
Esempio n. 16
0
    def __init__(self, data, model=None, **kwargs):
        # Dropdown menu of all subclasses of Model, incl. user-defined.
        self.models_menu = Dropdown()
        all_models = {m.__name__: m for m in Model.__subclasses__()}
        self.models_menu.values = all_models
        self.models_menu.on_trait_change(self._on_model_value_change,
                                             'value')
        # Button to trigger fitting.
        self.fit_button = Button(description='Fit')
        self.fit_button.on_click(self._on_fit_button_click)

        # Button to trigger guessing.
        self.guess_button = Button(description='Auto-Guess')
        self.guess_button.on_click(self._on_guess_button_click)

        # Parameter widgets are not built here. They are (re-)built when
        # the model is (re-)set.
        super(NotebookFitter, self).__init__(data, model, **kwargs)
 def create_case_widget(self):
     controller = self.controller 
     if controller.ds is None:
         return
     by_signature = Checkbox(
                     description='Cases indicated by signature?', 
                     value=self.by_signature
                     )
     case_id = Text(description='* Analysis for case:')        
     if 'biological_constraints' not in controller.options:
         bio_constraints = ''
     else:
         bio_constraints = ', '.join(controller.defaults('biological_constraints')) 
     constraints = Textarea(description='Biological constraints:',
                                          value=bio_constraints)
     button = Button(description='Create Analysis')
     button.on_click(self.create_report)
     button.case_id = case_id
     button.by_signature = by_signature
     button.constraints = constraints
     wi = VBox(children=[by_signature,
                                            case_id,
                                            constraints,
                                            button])
     return ('Analyze Case', wi)
 def update_info(self):
     
     title = HTML(value='<b> Cases to Co-localize </b>')
     buttons = []
     html_str = '<div><b>Is Valid: {0}</b></div>'.format(self.ci.is_valid())
     if self.ci.is_valid() is False:
         self.make_plot.disabled = True
     else:
         self.make_plot.disabled = False
     valid = HTML(value = html_str)
     html_str = '<table><caption> Auxiliary variables for ' + self.name 
     html_str += ' with ' + ', '.join(self.slice_variables) 
     html_str += ' as the slice variable{0}.</caption>'.format('s' if len(self.slice_variables) > 1 else '')
     html_str += '<tr ><th rowspan="2" align=center  style="padding:0 15px 0 15px;"> Slice<br> Variables </th>'
     html_str += '<th colspan="' + str(len(self.cases)) + '" align=center  style="padding:0 15px 0 15px;"> Cases </th></tr><tr align=center>'
     for c in self.cases:
             html_str += '<td style="padding:0 15px 0 15px;"><b>  {0}  </b></td>'.format(c.case_number)
     html_str += '</tr>\n'
     pset = self.ci.valid_interior_parameter_set()
     for i in self.cases:
         key = i.case_number
         case_button = Button(description='Case ' + key)
         buttons.append(case_button)
         case_button.pvals = pset[key] if key in pset else None
         case_button.on_click(self.open_case)
     for j, xj in enumerate(self.slice_variables):
         html_str += '<tr align=center><td>{0}</td>'.format(xj)
         for i, c in enumerate(self.cases):
             html_str += '<td>${0}_{1}</td>'.format(xj, i)
         html_str += '</tr>'  
     html_str += '</table>'
     html_str += '<caption>Case co-localization assumes that the slice variables '
     html_str += 'for one case are independent from the slice variables for the other cases in the co-localization.'
     html_str += 'Each auxiliary variable corresponds to a slice variable for one cases.</caption>'
     save_table = Button(description='Save variable table')
     save_table.on_click(self.save_table)
     save_table.table_data = html_str
     variables = HTML(value=html_str)
     self.info.children = [title] + buttons + [valid, save_table, variables]
 def create_case_widget(self):
     controller = self.controller 
     if controller.ds is None:
         return
     case = self.case
     self.info = VBox()
     self.equations = VBox()
     self.log_gains = VBox()
     self.parameter_table = VBox() 
     self.tolerances_table = VBox() 
     self.bounding_box_table = VBox()
     calculate_pvals = Button(description='Determine values for the parameters')
     calculate_pvals.visible = False
     calculate_pvals.on_click(self.identify_parameters)
     if case.is_valid() is True and case.is_cyclical is False:
         if self.pvals is None:
             calculate_pvals.visible = True
     close_button = Button(description='Close Tab')
     close_button.on_click(self.close_widget)
     wi = Popup(children=[self.info, 
                                        self.equations,
                                        self.log_gains,
                                        self.bounding_box_table,
                                        calculate_pvals,
                                        self.parameter_table,
                                        self.tolerances_table,
                                        close_button])
     if ipy_old is True:
         wi.set_css('height', '400px')
     else:
         wi.width = '700px'
     self.update_display()
     subtitle = self.subtitle
     if subtitle != '':
         subtitle = ' (' + subtitle + ')'
     self.title = 'Case ' + self.case.case_number + subtitle
     controller.update_child(self.title, wi)
 def save_popup_widget(self):
     controller = self.controller
     save_button = Button(description='Save')
     cancel_button = Button(description='Cancel')
     name_field = Text(description='* Name', value=controller.name)
     version_field = Text(description='Version', 
                          value=controller.version)
     save_button.on_click(self.save_widget_data)
     save_button.name_field = name_field
     save_button.version_field = version_field
     cancel_button.on_click(self.cancel_save)
     self.save_data = VBox(description='Save Data',
                            children=[VBox(children=[
                                            HTML(value='<center width="100%"><b>Are you sure you want to save?</b></center>'),
                                            name_field,
                                            version_field,
                                            HBox(children=[
                                                  save_button, 
                                                  cancel_button])])])
     self.save_data.box_style = 'warning'
     self.save_data.overflow_x = 'auto'
     self.save_data.overflow_y = 'auto'
     self.save_data.width = '100vh'
     return self.save_data
 def colocalization_widget(self):
     controller = self.controller
     cases = Textarea(description='* Cases to co-localize:')
     by_signature = Checkbox(description='Cases indicated by signature?', 
                                           value=self.by_signature)
     slice_variables = Textarea(description='* Slice variables:')
     if 'biological_constraints' not in controller.options:
         bio_constraints = ''
     else:
         bio_constraints = ', '.join(controller.defaults('biological_constraints')) 
     constraints = Textarea(description='Biological constraints:',
                                          value=bio_constraints)
     button = Button(description='Create Co-Localization')
     button.on_click(self.make_colocalization)
     button.cases = cases
     button.by_signature = by_signature
     button.slice_variables = slice_variables
     button.constraints = constraints
     wi = VBox(children=[cases,
                              by_signature,
                              slice_variables,
                              constraints,
                              button])
     return ('Co-localizations', wi)
 def create_plot_widget(self):
     controller = self.controller
     options = self.controller.options
     if controller.ds is None:
         return
     xaxis = controller.defaults('xaxis')
     if xaxis is None:
         xaxis = controller.ds.independent_variables[0]
     yaxis = controller.defaults('yaxis')
     if yaxis is None:
         yaxis = controller.ds.independent_variables[1]
     center = controller.defaults('center_axes')
     range_x = controller.defaults('range_x')
     range_y = controller.defaults('range_y') 
     xlabel = Dropdown(
               description='* X-Axis',
               values=controller.ds.independent_variables, 
               options=controller.ds.independent_variables, 
               value=xaxis)
     ylabel = Dropdown(
               description='* Y-Axis',
               values=['None'] + controller.ds.independent_variables,
               options=['None'] + controller.ds.independent_variables,
               value=yaxis)
     xmin = FloatText(description='* X-Min',
                                    value=range_x[0])
     xmax = FloatText(description='* X-Max',
                                    value=range_x[1])
     ymin = FloatText(description='* Y-Min',
                                    value=range_y[0])
     ymax = FloatText(description='* Y-Max',
                                    value=range_y[1])
     center_axes = Checkbox(description='Center Axes',
                                          value=center)
     boundaries = Checkbox(description='Draw Boundaries',
                                         value=False)
     plot_type = Dropdown(description='* Plot Type',
                          values=self.widget_types,
                          options=self.widget_types,
                          value='Design Space (interactive)')
     title_widget = Text(description='Title')
     caption_widget = Textarea(description='Caption')
     included = controller.defaults('included_cases')
     if included is None:
         included = []
     if isinstance(included, list) is False:
         included = [included]
     included_widget = Textarea(description='Only Cases',
                                              value=', '.join(included))
     wi = VBox(children=[xlabel, ylabel, plot_type, 
                                            xmin, xmax, ymin, ymax,
                                            center_axes, boundaries,
                                            title_widget, caption_widget,
                                            included_widget])
     for i in [xlabel, ylabel, plot_type]:
         i.on_trait_change(self.update_field, 'value')    
     plot_type.widget_container = wi
     button = Button(value=False, description='Add Plot')
     button.on_click(self.make_plot)
     button.xlabel = xlabel
     button.ylabel = ylabel
     button.xmin = xmin
     button.xmax = xmax
     button.ymin = ymin
     button.ymax = ymax
     button.center_axes = center_axes
     button.boundaries = boundaries
     button.plot_type = plot_type
     button.title = title_widget
     button.caption = caption_widget
     button.included = included_widget
     button.wi = wi
     self.title = title_widget
     self.caption = caption_widget
     self.boundaries = boundaries
     self.plot_type = plot_type
     self.xlabel = xlabel
     self.ylabel = ylabel
     self.ymin = ymin
     self.ymax = ymax
     self.xmin = xmin
     self.xmax = xmax
     add_plot = VBox(description='Add Plot',
                                        children=[wi,
                                                  self.plot_data, 
                                                  button])
     self.update_plot_widget('value', 'Design Space (Interactive)')
     return ('Create Plot', add_plot)
Esempio n. 23
0
class NotebookFitter(BaseFitter):
    __doc__ = _COMMON_DOC + """
    If IPython is available, it uses the IPython notebook's rich display
    to fit data interactively in a web-based GUI. The Parameters are
    represented in a web-based form that is kept in sync with `current_params`. 
    All subclasses to Model, including user-defined ones, are shown in a
    drop-down menu.

    Clicking the "Fit" button updates a plot, as above, and updates the
    Parameters in the form to reflect the best fit.
    """ + _COMMON_EXAMPLES_DOC

    def __init__(self, data, model=None, **kwargs):
        # Dropdown menu of all subclasses of Model, incl. user-defined.
        self.models_menu = Dropdown()
        all_models = {m.__name__: m for m in Model.__subclasses__()}
        self.models_menu.values = all_models
        self.models_menu.on_trait_change(self._on_model_value_change,
                                             'value')
        # Button to trigger fitting.
        self.fit_button = Button(description='Fit')
        self.fit_button.on_click(self._on_fit_button_click)

        # Button to trigger guessing.
        self.guess_button = Button(description='Auto-Guess')
        self.guess_button.on_click(self._on_guess_button_click)

        # Parameter widgets are not built here. They are (re-)built when
        # the model is (re-)set.
        super(NotebookFitter, self).__init__(data, model, **kwargs)

    def _repr_html_(self):
        display(self.models_menu)
        button_box = Box()
        button_box.children = [self.fit_button, self.guess_button]
        display(button_box)
        button_box.add_class('hbox')
        for pw in self.param_widgets:
            display(pw)
        self.plot()

    def guess(self):
        guessing_successful = super(NotebookFitter, self).guess()
        self.guess_button.disabled = not guessing_successful

    def _finalize_model(self, value):
        first_run = not hasattr(self, 'param_widgets')
        if not first_run:
            # Remove all Parameter widgets, and replace them with widgets
            # for the new model.
            for pw in self.param_widgets:
                pw.close()
        self.models_menu.value = value 
        self.param_widgets = [ParameterWidgetGroup(p)
                              for _, p in self._current_params.items()]
        if not first_run:
            for pw in self.param_widgets:
                display(pw)

    def _finalize_params(self):
        for pw in self.param_widgets:
            pw.value = self._current_params[pw.name].value
            pw.min = self._current_params[pw.name].min
            pw.max = self._current_params[pw.name].max
            pw.vary = self._current_params[pw.name].vary

    def plot(self):
        clear_output(wait=True)
        super(NotebookFitter, self).plot()

    def fit(self):
        super(NotebookFitter, self).fit()
        self.plot()
Esempio n. 24
0
File: cirq.py Progetto: embray/cirq
    def __init__(self, domains, components, circuit=None, **kwargs):
        super(CircuitBuilder, self).__init__(**kwargs)
        self.button_text = "Launch CircuitBuilder"
        self.description = circuit.name

        def _resize_inputs(el):
            el.set_css({"width": "100px"})


        #####  make controls
        self.basic_controls = HorizontalContainer()
        self.add_component_controls = HorizontalContainer()
        self.add_port_controls = HorizontalContainer()
        self.change_component_controls = HorizontalContainer()
        self.change_port_controls = HorizontalContainer()

        # 1) basic controls
        self.reset_view_btn = ButtonWidget(description="Reset view")
        self.reset_view_btn.on_click(self.reset_view)

        self.circname = TextWidget(description="Circuit name", value=circuit.name)
        self.circname.on_displayed(_resize_inputs)

        self.rename_circ_btn = ButtonWidget(description="Rename")
        self.rename_circ_btn.on_click(self.rename_circuit)
        self.add_port_btn = ButtonWidget(description="New Port")
        self.add_port_btn.on_click(self.show_add_port_controls)
        self.add_comp_btn = ButtonWidget(description="New Component")
        self.add_comp_btn.on_click((self.show_add_comp_controls))
        # self.download_btn = ButtonWidget(description="SaveAsSVG")
        # self.download_btn.on_click()

        self.basic_controls.children = [
            self.reset_view_btn,
            self.circname,
            self.rename_circ_btn,
            self.add_port_btn,
            self.add_comp_btn,
        ]

        # 2) add component
        self.add_comp_type = DropdownWidget(description="ComponentType")
        self.add_comp_name = TextWidget(description="Component name")
        self.add_comp_name.on_displayed(_resize_inputs)

        self.add_comp_add = ButtonWidget(description="Add Component")
        self.add_comp_add.on_click(self.add_component)

        self.add_comp_back = ButtonWidget(description="Back")
        self.add_comp_back.on_click(self.back)

        self.add_component_controls.children = [
            self.add_comp_type,
            self.add_comp_name,
            self.add_comp_add,
            self.add_comp_back,
        ]

        # 3) add port
        self.add_port_name = TextWidget(description="Port name")
        self.add_port_name.on_displayed(_resize_inputs)

        self.add_port_domain = DropdownWidget(description="Domain")
        self.add_port_domain.on_trait_change(self._update_port_directions, "value_name")
        self.add_port_direction = DropdownWidget(description="Direction",
                                                 values={"in": "Input", "out": "Output"})
        self.add_port_add = ButtonWidget(description="Add Port")
        self.add_port_add.on_click(self.add_port)
        self.add_port_back = ButtonWidget(description="Back")
        self.add_port_back.on_click(self.back)

        self.add_port_controls.children = [
            self.add_port_name,
            self.add_port_domain,
            self.add_port_direction,
            self.add_port_add,
            self.add_port_back,
        ]

        # 4) change component
        self.mod_comp_name = TextWidget(description="Component name")
        self.mod_comp_name.on_displayed(_resize_inputs)
        self.mod_comp_rename = ButtonWidget(description="Rename")
        self.mod_comp_rename.on_click(self.rename_component)
        self.mod_comp_delete = ButtonWidget(description="Delete")
        self.mod_comp_delete.on_click(self.delete_component)
        self.mod_comp_back = ButtonWidget(description="Back")
        self.mod_comp_back.on_click(self.back)

        self.change_component_controls.children = [
            self.mod_comp_name,
            self.mod_comp_rename,
            self.mod_comp_delete,
            self.mod_comp_back,
        ]

        # 5) change port
        self.mod_port_name = TextWidget(description="Port name")
        self.mod_port_name.on_displayed(_resize_inputs)
        self.mod_port_rename = ButtonWidget(description="Rename")
        self.mod_port_rename.on_click(self.rename_port)
        self.mod_port_dec = ButtonWidget(description="<")
        self.mod_port_inc = ButtonWidget(description=">")
        self.mod_port_dec.on_click(self.dec_port_order)
        self.mod_port_inc.on_click(self.inc_port_order)

        self.mod_port_delete = ButtonWidget(description="Delete")
        self.mod_port_delete.on_click(self.delete_port)
        self.mod_port_back = ButtonWidget(description="Back")
        self.mod_port_back.on_click(self.back)

        self.change_port_controls.children = [
            self.mod_port_name,
            self.mod_port_rename,
            self.mod_port_dec,
            self.mod_port_inc,
            self.mod_port_delete,
            self.mod_port_back,
        ]

        # has to come at end!!
        self.domains = domains
        self.components = components
        self.circuit = circuit

        self.circuit.on_trait_change(self._handle_circuit_selection, "selected_element")
        self.circuit.on_trait_change(self._handle_circuit_name, "name")


        self.children = [
            self.basic_controls,
            self.add_component_controls,
            self.add_port_controls,
            self.change_component_controls,
            self.change_port_controls,
            self.circuit
        ]
        self.back()
Esempio n. 25
0
File: cirq.py Progetto: embray/cirq
class CircuitBuilder(PopupWidget):
    """
    Adds additional controls to the circuit widget for
    adding/removing external ports and component instances.
    """
    _view_name = Unicode("CircuitBuilderView", sync=True)
    circuit = Instance(klass=Circuit, sync=True)
    domains = List()
    _domains_by_name = Dict()
    components = List()
    _components_by_name = Dict()

    def _components_changed(self, name, old, new):
        self.add_comp_type.values = {c.name: c.name for c in new}
        self._components_by_name = {c.name: c for c in new}

    def _domains_changed(self, name, old, new):
        self.add_port_domain.values = {d.name: d.name for d in new}
        self.add_port_domain.value_name = new[0].name
        self._domains_by_name = {d.name: d for d in new}
        self._update_port_directions()

    def __init__(self, domains, components, circuit=None, **kwargs):
        super(CircuitBuilder, self).__init__(**kwargs)
        self.button_text = "Launch CircuitBuilder"
        self.description = circuit.name

        def _resize_inputs(el):
            el.set_css({"width": "100px"})


        #####  make controls
        self.basic_controls = HorizontalContainer()
        self.add_component_controls = HorizontalContainer()
        self.add_port_controls = HorizontalContainer()
        self.change_component_controls = HorizontalContainer()
        self.change_port_controls = HorizontalContainer()

        # 1) basic controls
        self.reset_view_btn = ButtonWidget(description="Reset view")
        self.reset_view_btn.on_click(self.reset_view)

        self.circname = TextWidget(description="Circuit name", value=circuit.name)
        self.circname.on_displayed(_resize_inputs)

        self.rename_circ_btn = ButtonWidget(description="Rename")
        self.rename_circ_btn.on_click(self.rename_circuit)
        self.add_port_btn = ButtonWidget(description="New Port")
        self.add_port_btn.on_click(self.show_add_port_controls)
        self.add_comp_btn = ButtonWidget(description="New Component")
        self.add_comp_btn.on_click((self.show_add_comp_controls))
        # self.download_btn = ButtonWidget(description="SaveAsSVG")
        # self.download_btn.on_click()

        self.basic_controls.children = [
            self.reset_view_btn,
            self.circname,
            self.rename_circ_btn,
            self.add_port_btn,
            self.add_comp_btn,
        ]

        # 2) add component
        self.add_comp_type = DropdownWidget(description="ComponentType")
        self.add_comp_name = TextWidget(description="Component name")
        self.add_comp_name.on_displayed(_resize_inputs)

        self.add_comp_add = ButtonWidget(description="Add Component")
        self.add_comp_add.on_click(self.add_component)

        self.add_comp_back = ButtonWidget(description="Back")
        self.add_comp_back.on_click(self.back)

        self.add_component_controls.children = [
            self.add_comp_type,
            self.add_comp_name,
            self.add_comp_add,
            self.add_comp_back,
        ]

        # 3) add port
        self.add_port_name = TextWidget(description="Port name")
        self.add_port_name.on_displayed(_resize_inputs)

        self.add_port_domain = DropdownWidget(description="Domain")
        self.add_port_domain.on_trait_change(self._update_port_directions, "value_name")
        self.add_port_direction = DropdownWidget(description="Direction",
                                                 values={"in": "Input", "out": "Output"})
        self.add_port_add = ButtonWidget(description="Add Port")
        self.add_port_add.on_click(self.add_port)
        self.add_port_back = ButtonWidget(description="Back")
        self.add_port_back.on_click(self.back)

        self.add_port_controls.children = [
            self.add_port_name,
            self.add_port_domain,
            self.add_port_direction,
            self.add_port_add,
            self.add_port_back,
        ]

        # 4) change component
        self.mod_comp_name = TextWidget(description="Component name")
        self.mod_comp_name.on_displayed(_resize_inputs)
        self.mod_comp_rename = ButtonWidget(description="Rename")
        self.mod_comp_rename.on_click(self.rename_component)
        self.mod_comp_delete = ButtonWidget(description="Delete")
        self.mod_comp_delete.on_click(self.delete_component)
        self.mod_comp_back = ButtonWidget(description="Back")
        self.mod_comp_back.on_click(self.back)

        self.change_component_controls.children = [
            self.mod_comp_name,
            self.mod_comp_rename,
            self.mod_comp_delete,
            self.mod_comp_back,
        ]

        # 5) change port
        self.mod_port_name = TextWidget(description="Port name")
        self.mod_port_name.on_displayed(_resize_inputs)
        self.mod_port_rename = ButtonWidget(description="Rename")
        self.mod_port_rename.on_click(self.rename_port)
        self.mod_port_dec = ButtonWidget(description="<")
        self.mod_port_inc = ButtonWidget(description=">")
        self.mod_port_dec.on_click(self.dec_port_order)
        self.mod_port_inc.on_click(self.inc_port_order)

        self.mod_port_delete = ButtonWidget(description="Delete")
        self.mod_port_delete.on_click(self.delete_port)
        self.mod_port_back = ButtonWidget(description="Back")
        self.mod_port_back.on_click(self.back)

        self.change_port_controls.children = [
            self.mod_port_name,
            self.mod_port_rename,
            self.mod_port_dec,
            self.mod_port_inc,
            self.mod_port_delete,
            self.mod_port_back,
        ]

        # has to come at end!!
        self.domains = domains
        self.components = components
        self.circuit = circuit

        self.circuit.on_trait_change(self._handle_circuit_selection, "selected_element")
        self.circuit.on_trait_change(self._handle_circuit_name, "name")


        self.children = [
            self.basic_controls,
            self.add_component_controls,
            self.add_port_controls,
            self.change_component_controls,
            self.change_port_controls,
            self.circuit
        ]
        self.back()



    def _handle_circuit_selection(self):
        e = self.circuit.selected_element
        if e:
            if isinstance(e, Port) and e.is_ext:
                self.back()
                self.show_mod_port_controls(e)
                return
            elif isinstance(e, ComponentInstance):
                self.back()
                self.show_mod_comp_controls(e)
                return
        self.back()

    def _handle_circuit_name(self):
        self.circname.value = self.circuit.name
        self.description = self.circuit.name

    def reset_view(self, *_):
        self.circuit.zoom = (0., 0., 1.)

    def back(self, *_):
        for c in [
            self.add_component_controls,
            self.add_port_controls,
            self.change_component_controls,
            self.change_port_controls,
        ]:
            c.visible = False
        self.basic_controls.visible = True

    def show_mod_comp_controls(self, c):
        self.basic_controls.visible = False
        self.change_component_controls.visible = True
        self.mod_comp_name.value = c.name

    def show_mod_port_controls(self, p):
        self.basic_controls.visible = False
        self.change_port_controls.visible = True
        self.mod_port_name.value = p.name

    def show_add_port_controls(self, *_):
        self.basic_controls.visible = False
        self.add_port_controls.visible = True

    def show_add_comp_controls(self, *_):
        self.basic_controls.visible = False
        self.add_component_controls.visible = True

    def _update_port_directions(self):
        d = self._domains_by_name.get(self.add_port_domain.value_name, False)
        if isinstance(d, Domain):
            self.add_port_direction.visible = d.causal

    def rename_circuit(self, *_):
        if len(self.circname.value):
            self.circuit.name = self.circname.value

    def add_component(self, *_):
        ctype = self._components_by_name[self.add_comp_type.value_name]
        cname = self.add_comp_name.value

        if len(cname) and not cname in self.circuit.c:

            new_comp = ctype.make_instance(cname)
            self.circuit.component_instances = self.circuit.component_instances + [new_comp]

    def add_port(self, *_):
        d = self._domains_by_name[self.add_port_domain.value_name]
        dir = self.add_port_direction.value_name
        if not d.causal:
            dir = "inout"
        pname = self.add_port_name.value
        if len(pname) and not pname in self.circuit.p:
            newp = Port(name=pname, domain=d, direction=dir)
            self.circuit.ports = self.circuit.ports + [newp]


    def rename_component(self, *_):
        c = self.circuit.selected_element
        if not isinstance(c, ComponentInstance):
            return
        newname = self.mod_comp_name.value
        if len(newname) and not newname in self.circuit.c:
            del self.circuit.c[c.name]
            c.name = newname
            self.circuit.c[c.name] = c

    def rename_port(self, *_):
        p = self.circuit.selected_element
        if not isinstance(p, Port):
            return
        newname = self.mod_port_name.value
        if len(newname) and not newname in self.circuit.p:
            del self.circuit.p[p.name]
            p.name = newname
            self.circuit.p[p.name] = p

    def delete_component(self, *_):
        c = self.circuit.selected_element
        if not isinstance(c, ComponentInstance) \
                or not c in self.circuit.component_instances:
            return
        self.circuit.component_instances = filter(lambda cc: cc is not c,
                                                  self.circuit.component_instances)
        for p in c.ports:
            for cc in p.connections_in + p.connections_out:
                cc.remove()

        self.circuit.selected_element = None

    def delete_port(self, *_):
        p = self.circuit.selected_element
        if not isinstance(p, Port) \
                or not p in self.circuit.ports:
            return
        self.circuit.ports = filter(lambda pp: pp is not p,
                                                  self.circuit.ports)

        for c in p.connections_in + p.connections_out:
            c.remove()

        self.circuit.selected_element = None


    def dec_port_order(self, *_):
        p = self.circuit.selected_element
        if not isinstance(p, Port) \
                or not p in self.circuit.ports:
            return
        ps = list(self.circuit.ports)
        ii = ps.index(p)
        ps.pop(ii)
        ii = max(1, ii)
        self.circuit.ports = ps[:ii-1] + [p] + ps[ii-1:]


    def inc_port_order(self, *_):
        p = self.circuit.selected_element
        if not isinstance(p, Port) \
                or not p in self.circuit.ports:
            return
        ps = list(self.circuit.ports)
        ii = ps.index(p)
        ps.pop(ii)
        self.circuit.ports = ps[:ii+1] + [p] + ps[ii+1:]
        
    def increment_face(self):
        if self.index + 1 >= len(self.imgs):
            return self.index
        else:
            while str(self.index) in self.results:
                print self.index
                self.index += 1
            return self.index
    
    def record_result(self, smile=True):
        self.results[str(self.index)] = smile

trainer = Trainer()

button_smile = ButtonWidget(description='smile')
button_no_smile = ButtonWidget(description='sad face')

def display_face(face):
    clear_output()
    imshow(face, cmap='gray')
    axis('off')

def update_smile(b):
    trainer.record_result(smile=True)
    trainer.increment_face()
    display_face(trainer.imgs[trainer.index])

def update_no_smile(b):
    trainer.record_result(smile=False)
    trainer.increment_face()
Esempio n. 27
0
    def interactive_plot_modalities_lavalamps(
        self,
        sample_subsets=None,
        feature_subsets=None,
        color=red,
        x_offset=0,
        use_these_modalities=True,
        bootstrapped=False,
        bootstrapped_kws=None,
        savefile="",
    ):
        def do_interact(
            sample_subset=None,
            feature_subset=None,
            color=red,
            x_offset=0,
            use_these_modalities=True,
            bootstrapped=False,
            bootstrapped_kws=None,
            savefile="",
        ):

            for k, v in locals().iteritems():
                if k == "self":
                    continue
                sys.stdout.write("{} : {}\n".format(k, v))

            assert feature_subset in self.splicing.feature_subsets.keys()
            feature_ids = self.splicing.feature_subsets[feature_subset]

            from sklearn.preprocessing import LabelEncoder

            le = LabelEncoder()
            n_in_this_class = len(set(le.fit_transform(self.experiment_design.data[sample_subset])))

            try:
                assert n_in_this_class
            except:
                raise RuntimeError("this sample designator is not binary")

            # TODO: cast non-boolean binary ids to boolean
            dtype = self.experiment_design.data[sample_subset].dtype
            try:
                assert dtype == "bool"
            except:
                raise RuntimeError("this sample designator is not boolean")

            sample_ids = self.experiment_design.data[sample_subset].index[self.experiment_design.data[sample_subset]]

            self.splicing.plot_modalities_lavalamps(
                sample_ids,
                feature_ids,
                color=color,
                x_offset=x_offset,
                use_these_modalities=use_these_modalities,
                bootstrapped=bootstrapped,
                bootstrapped_kws=bootstrapped_kws,
                ax=None,
            )
            plt.tight_layout()

        if feature_subsets is None:
            feature_subsets = Interactive.get_feature_subsets(self, ["splicing"])

        if sample_subsets is None:
            sample_subsets = self.default_sample_subsets

        if bootstrapped_kws is None:
            bootstrapped_kws = {}

        gui = interact(
            do_interact,
            sample_subset=sample_subsets,
            feature_subset=feature_subsets,
            color=color,
            x_offset=x_offset,
            use_these_modalities=use_these_modalities,
            bootstrapped=bootstrapped,
            bootstrapped_kws=bootstrapped_kws,
            savefile=savefile,
        )

        def save(w):
            filename, extension = os.path.splitext(savefile.value)
            self.maybe_make_directory(savefile.value)
            gui.widget.result.savefig(savefile.value, format=extension.lstrip("."))

        savefile = TextWidget(description="savefile", value="figures/clustermap.pdf")
        save_widget = ButtonWidget(description="save")
        gui.widget.children = list(gui.widget.children) + [savefile, save_widget]
        save_widget.on_click(save)
Esempio n. 28
0
    def interactive_correlations(self):
        def do_interact(
            data_type="expression",
            sample_subset=self.default_sample_subsets,
            feature_subset=self.default_feature_subset,
            metric="euclidean",
            method="average",
            list_link="",
            scale_fig_by_data=True,
            fig_width="",
            fig_height="",
            featurewise=False,
        ):

            for k, v in locals().iteritems():
                if k == "self":
                    continue
                sys.stdout.write("{} : {}\n".format(k, v))

            if feature_subset != "custom" and list_link != "":
                raise ValueError('set feature_subset to "custom" to use list_link')

            if feature_subset == "custom" and list_link == "":
                raise ValueError("use a custom list name please")

            if feature_subset == "custom":
                feature_subset = list_link
            elif feature_subset not in self.default_feature_subsets[data_type]:
                warnings.warn(
                    "This feature_subset ('{}') is not available in "
                    "this data type ('{}'). Falling back on all "
                    "features.".format(feature_subset, data_type)
                )
            return self.plot_correlations(
                sample_subset=sample_subset,
                feature_subset=feature_subset,
                data_type=data_type,
                scale_fig_by_data=scale_fig_by_data,
                method=method,
                metric=metric,
                featurewise=featurewise,
            )

        feature_subsets = Interactive.get_feature_subsets(self, ["expression", "splicing"])
        method = ("average", "weighted", "single", "complete", "ward")
        metric = (
            "euclidean",
            "seuclidean",
            "sqeuclidean",
            "chebyshev",
            "cosine",
            "cityblock",
            "mahalonobis",
            "minowski",
            "jaccard",
        )
        gui = interact(
            do_interact,
            data_type=("expression", "splicing"),
            sample_subset=self.default_sample_subsets,
            feature_subset=feature_subsets,
            metric=metric,
            method=method,
            featurewise=False,
        )

        def save(w):
            filename, extension = os.path.splitext(savefile.value)
            self.maybe_make_directory(savefile.value)
            gui.widget.result.savefig(savefile.value, format=extension.lstrip("."))

        savefile = TextWidget(description="savefile", value="figures/correlations.pdf")
        save_widget = ButtonWidget(description="save")
        gui.widget.children = list(gui.widget.children) + [savefile, save_widget]
        save_widget.on_click(save)
        return gui
Esempio n. 29
0
class NotebookFitter(MPLFitter):
    __doc__ = _COMMON_DOC + """
    If IPython is available, it uses the IPython notebook's rich display
    to fit data interactively in a web-based GUI. The Parameters are
    represented in a web-based form that is kept in sync with `current_params`.
    All subclasses to Model, including user-defined ones, are shown in a
    drop-down menu.

    Clicking the "Fit" button updates a plot, as above, and updates the
    Parameters in the form to reflect the best fit.

    Parameters
    ----------
    data : array-like
    model : lmfit.Model
        optional initial Model to use, maybe be set or changed later
    all_models : list
        optional list of Models to populate drop-down menu, by default
        all built-in and user-defined subclasses of Model are used

    Additional Parameters
    ---------------------
    axes_style : dictionary representing style keyword arguments to be
        passed through to `Axes.set(...)`
    data_style : dictionary representing style keyword arguments to be passed
        through to the matplotlib `plot()` command the plots the data points
    init_style : dictionary representing style keyword arguments to be passed
        through to the matplotlib `plot()` command the plots the initial fit
        line
    best_style : dictionary representing style keyword arguments to be passed
        through to the matplotlib `plot()` command the plots the best fit
        line
    **kwargs : independent variables or extra arguments, passed like `x=x`
    """ + _COMMON_EXAMPLES_DOC

    def __init__(self,
                 data,
                 model=None,
                 all_models=None,
                 axes_style={},
                 data_style={},
                 init_style={},
                 best_style={},
                 **kwargs):
        # Dropdown menu of all subclasses of Model, incl. user-defined.
        self.models_menu = Dropdown()
        # Dropbox API is very different between IPy 2.x and 3.x.
        if IPY2:
            if all_models is None:
                all_models = {m.__name__: m for m in Model.__subclasses__()}
            self.models_menu.values = all_models
        else:
            if all_models is None:
                all_models = [(m.__name__, m) for m in Model.__subclasses__()]
            self.models_menu.options = all_models
        self.models_menu.on_trait_change(self._on_model_value_change, 'value')
        # Button to trigger fitting.
        self.fit_button = Button(description='Fit')
        self.fit_button.on_click(self._on_fit_button_click)

        # Button to trigger guessing.
        self.guess_button = Button(description='Auto-Guess')
        self.guess_button.on_click(self._on_guess_button_click)

        # Parameter widgets are not built here. They are (re-)built when
        # the model is (re-)set.
        super(NotebookFitter,
              self).__init__(data, model, axes_style, data_style, init_style,
                             best_style, **kwargs)

    def _repr_html_(self):
        display(self.models_menu)
        button_box = HBox()
        button_box.children = [self.fit_button, self.guess_button]
        display(button_box)
        for pw in self.param_widgets:
            display(pw)
        self.plot()

    def guess(self):
        guessing_successful = super(NotebookFitter, self).guess()
        self.guess_button.disabled = not guessing_successful

    def _finalize_model(self, value):
        first_run = not hasattr(self, 'param_widgets')
        if not first_run:
            # Remove all Parameter widgets, and replace them with widgets
            # for the new model.
            for pw in self.param_widgets:
                pw.close()
        self.models_menu.value = value
        self.param_widgets = [
            ParameterWidgetGroup(p) for _, p in self._current_params.items()
        ]
        if not first_run:
            for pw in self.param_widgets:
                display(pw)

    def _finalize_params(self):
        for pw in self.param_widgets:
            pw.value = self._current_params[pw.name].value
            pw.min = self._current_params[pw.name].min
            pw.max = self._current_params[pw.name].max
            pw.vary = self._current_params[pw.name].vary

    def plot(self):
        clear_output(wait=True)
        super(NotebookFitter, self).plot()

    def fit(self):
        super(NotebookFitter, self).fit()
        self.plot()
Esempio n. 30
0
    def interactive_pca(
        self,
        data_types=("expression", "splicing"),
        sample_subsets=None,
        feature_subsets=None,
        color_samples_by=None,
        featurewise=False,
        x_pc=(1, 10),
        y_pc=(1, 10),
        show_point_labels=False,
        list_link="",
        plot_violins=False,
        scale_by_variance=True,
        savefile="figures/last.pca.pdf",
    ):
        def do_interact(
            data_type="expression",
            sample_subset=self.default_sample_subsets,
            feature_subset=self.default_feature_subsets,
            featurewise=False,
            list_link="",
            x_pc=1,
            y_pc=2,
            plot_violins=False,
            show_point_labels=False,
            color_samples_by=self.metadata.phenotype_col,
            bokeh=False,
            scale_by_variance=True,
            most_variant_features=False,
            std_multiplier=(0, 5.0),
        ):
            for k, v in locals().iteritems():
                if k == "self":
                    continue
                sys.stdout.write("{} : {}\n".format(k, v))

            if feature_subset != "custom" and list_link != "":
                raise ValueError('Set feature_subset to "custom" to use list_link')

            if feature_subset == "custom" and list_link == "":
                raise ValueError("Use a custom list name please")

            if feature_subset == "custom":
                feature_subset = link_to_list(list_link)

            elif feature_subset not in self.default_feature_subsets[data_type]:
                warnings.warn(
                    "This feature_subset ('{}') is not available in "
                    "this data type ('{}'). Falling back on all "
                    "features.".format(feature_subset, data_type)
                )

            return self.plot_pca(
                sample_subset=sample_subset,
                data_type=data_type,
                featurewise=featurewise,
                x_pc=x_pc,
                y_pc=y_pc,
                show_point_labels=show_point_labels,
                feature_subset=feature_subset,
                plot_violins=plot_violins,
                color_samples_by=color_samples_by,
                bokeh=bokeh,
                std_multiplier=std_multiplier,
                scale_by_variance=scale_by_variance,
                most_variant_features=most_variant_features,
            )

        # self.plot_study_sample_legend()

        if feature_subsets is None:
            feature_subsets = Interactive.get_feature_subsets(self, data_types)

        if sample_subsets is None:
            sample_subsets = self.default_sample_subsets

        color_samples_by = self.metadata.data.columns.tolist()

        gui = interact(
            do_interact,
            data_type=data_types,
            sample_subset=sample_subsets,
            feature_subset=feature_subsets + ["custom"],
            featurewise=featurewise,
            x_pc=x_pc,
            y_pc=y_pc,
            show_point_labels=show_point_labels,
            list_link=list_link,
            plot_violins=plot_violins,
            color_samples_by=color_samples_by,
            scale_by_variance=scale_by_variance,
        )

        def save(w):
            # Make the directory if it's not already there
            filename, extension = os.path.splitext(savefile.value)
            self.maybe_make_directory(savefile.value)

            gui.widget.result.fig_reduced.savefig(savefile.value, format=extension)

            # add "violins" after the provided filename, but before the
            # extension
            violins_file = "{}.{}".format("_".join([filename, "violins"]), extension)
            try:
                gui.widget.result.fig_violins.savefig(violins_file, format=extension.lstrip("."))
            except AttributeError:
                pass

        savefile = TextWidget(description="savefile")
        save_widget = ButtonWidget(description="save")
        gui.widget.children = list(gui.widget.children) + [savefile, save_widget]
        save_widget.on_click(save)
        return gui
 def add_figure_widget(self, image_data, title='', caption = '', pvals=None, colors=None):
     children = [i for i in self.unsaved.children]      
     if len(title) > 0:
         title = title + '.'
     if len(caption) > 0:
         caption = '  ' + caption
     html_str = '<b>'+title+'</b>' + caption
     html_widget = HTML(value=html_str)
     save_button = Button(description='Save Figure')
     save_button.image_data = image_data
     save_button.title = title
     save_button.caption = caption
     save_button.on_click(self.save_unsaved_figure)
     save_button.pvals = pvals
     save_button.colors = colors
     close_button = Button(description='Remove Figure')
     close_button.on_click(self.remove_unsaved_figure)
     restore_pvals = Button(description='Restore Parameter Values')
     restore_pvals.pvals = pvals
     if pvals is None:
         restore_pvals.visible = False
     if old_ipython is True:
         image_widget = Image()
         image_widget.value = image_data
         tab_widget = VBox(children=[image_widget, html_widget])
         if colors is not None:
             html_widgets = self.colorbar_tabs(colors)
             tab_widget.description='Figure'
             tab_widget = Tab(children=[tab_widget]+html_widgets)
     else:
         image_widget = self._image_widget_new(image_data, colors=colors)
         tab_widget = VBox(children=[image_widget, html_widget])
         toggle = Button(description='Hide')
         
     restore_pvals.on_click(self.restore_figure_pvals)
     if old_ipython is True:
         wi = Popup(children=[close_button, save_button, tab_widget, restore_pvals])
     else:
         contents = VBox(children=[close_button, save_button, tab_widget, restore_pvals])
         contents.width = '100%'
         wi = Popup(children=[contents])
         wi.border = '1px solid black'
     save_button.wi = wi
     close_button.wi = wi
     children.append(wi)
     self.unsaved.children = children
     if colors is not None:
         if old_ipython is True:
             tab_widget.set_title(0, 'Figure')
             tab_widget.set_title(1, 'Colorbar')
class DisplayColocalization(object):
    
    def __init__(self, controller, cases, slice_variables):
        setattr(self, 'controller', controller)
        setattr(self, 'colocalizations_widget', None)
        setattr(self, 'cases', cases)
        setattr(self, 'slice_variables', slice_variables)
        setattr(self, 'name', 'Co-localization: ' + ', '.join([i.case_number for i in self.cases]))
        setattr(self, 'constraints', [])
        setattr(self, 'active_constraints', False)
        setattr(self, 'ci', dspace.CaseColocalization(cases, slice_variables))
        setattr(self, 'pvals', {})
        setattr(self, 'y_variable', 'log('+controller.ds.dependent_variables[0]+')')
        
    def make_display_widget(self):
        controller = self.controller 
        if controller.ds is None:
            return
        self.info = VBox()
        self.constraints_widget = VBox()
        self.plot = HBox()
        self.log_coordinates = True
        self.global_tolerance = VBox()
        close_button = Button(description='Close Tab')
        close_button.on_click(self.close_widget)
        ss_options = ['log('+ i + ')' for i in controller.ds.dependent_variables]
        self.y_dropdown = Dropdown(description='y-axis',
                                   values=ss_options,
                                   options=ss_options,
                                   value=self.y_variable)
        self.make_plot = Button(description='Create Plot')
        self.make_plot.on_click(self.change_y_axis)
        self.make_plot.yaxis = self.y_dropdown
        self.make_plot.visible = True
        check_box = Checkbox(description='Logarithmic coordinates', 
                                           value=self.log_coordinates)
        check_box.on_trait_change(self.update_log, 'value')
        self.y_dropdown.visible = False
        if len(self.slice_variables) <= 3:
            ## self.make_plot.visible = True
            if len(self.slice_variables) == 1:
                self.y_dropdown.visible = True
            if len(self.slice_variables) == 3:
                self.y_dropdown = HTML(value='<font color="red">Warning 3D plots are experimental.</font>')
        wi = VBox(children=[self.info, 
                            self.constraints_widget,
                            check_box,
                            self.y_dropdown,
                            self.make_plot,
                            self.global_tolerance,
                            close_button])
        if old_ipython is True:
            wi.set_css('height', '400px')
        else:
            wi.height='400px'
            wi.overflow_x = 'auto'
            wi.overflow_y = 'auto'

        self.update_display()
        controller.update_child(self.name, wi)
                
    def update_display(self):
        self.update_info()
        self.update_constraints()
        self.update_global_tolerances()
        
    def update_log(self, name, value):
        controller = self.controller
        self.log_coordinates = value
        if value == False:
            ss_old = ['log('+ i + ')' for i in controller.ds.dependent_variables]
            ss_new = [i for i in controller.ds.dependent_variables]
        else:
            ss_old = [i for i in controller.ds.dependent_variables]
            ss_new = ['log('+i+')' for i in controller.ds.dependent_variables]
        ss_options = OrderedDict([(unicode(i),i) for i in ss_new])
        index = ss_old.index(self.y_variable)
        self.y_variable = ss_new[index]
        self.y_dropdown.values = ss_options
        self.y_dropdown.options = ss_options
        self.y_dropdown.value = unicode(self.y_variable)
        self.update_display()
        
        
    def update_info(self):
        
        title = HTML(value='<b> Cases to Co-localize </b>')
        buttons = []
        html_str = '<div><b>Is Valid: {0}</b></div>'.format(self.ci.is_valid())
        if self.ci.is_valid() is False:
            self.make_plot.disabled = True
        else:
            self.make_plot.disabled = False
        valid = HTML(value = html_str)
        html_str = '<table><caption> Auxiliary variables for ' + self.name 
        html_str += ' with ' + ', '.join(self.slice_variables) 
        html_str += ' as the slice variable{0}.</caption>'.format('s' if len(self.slice_variables) > 1 else '')
        html_str += '<tr ><th rowspan="2" align=center  style="padding:0 15px 0 15px;"> Slice<br> Variables </th>'
        html_str += '<th colspan="' + str(len(self.cases)) + '" align=center  style="padding:0 15px 0 15px;"> Cases </th></tr><tr align=center>'
        for c in self.cases:
                html_str += '<td style="padding:0 15px 0 15px;"><b>  {0}  </b></td>'.format(c.case_number)
        html_str += '</tr>\n'
        pset = self.ci.valid_interior_parameter_set()
        for i in self.cases:
            key = i.case_number
            case_button = Button(description='Case ' + key)
            buttons.append(case_button)
            case_button.pvals = pset[key] if key in pset else None
            case_button.on_click(self.open_case)
        for j, xj in enumerate(self.slice_variables):
            html_str += '<tr align=center><td>{0}</td>'.format(xj)
            for i, c in enumerate(self.cases):
                html_str += '<td>${0}_{1}</td>'.format(xj, i)
            html_str += '</tr>'  
        html_str += '</table>'
        html_str += '<caption>Case co-localization assumes that the slice variables '
        html_str += 'for one case are independent from the slice variables for the other cases in the co-localization.'
        html_str += 'Each auxiliary variable corresponds to a slice variable for one cases.</caption>'
        save_table = Button(description='Save variable table')
        save_table.on_click(self.save_table)
        save_table.table_data = html_str
        variables = HTML(value=html_str)
        self.info.children = [title] + buttons + [valid, save_table, variables]
        
    def update_constraints(self):
        constraints_widget = Textarea(description='Constraints',
                                                    value = ',\n'.join(self.constraints)
                                                    )
        constraints_widget.visible = self.active_constraints
        button = Button(description='Done' if self.active_constraints else 'Modify constraints')
        button.constraints_widget = constraints_widget
        button.on_click(self.modify_constraints)
        self.constraints_widget.children = [constraints_widget, button]
        
    def modify_constraints(self, b):
        if self.active_constraints is False:
            self.active_constraints = True
        else:
            self.active_constraints = False
            self.constraints = str(b.constraints_widget.value).split(',')
            self.constraints = [i.strip() for i in self.constraints]
            self.ci = dspace.CaseColocalization(self.cases, 
                                                self.slice_variables,
                                                constraints = self.constraints)
            self.update_info()
            ## self.update_plot()
        self.update_constraints()
        
    def update_plot(self):
        controller = self.controller
        ds = controller.ds
        ## if len(self.slice_variables) > 3:
        ##     return
        ci = self.ci
        if ci.is_valid() is False:
            ## self.plot.children = []
            self.pvals = {}
            return
        pset = self.ci.valid_interior_parameter_set()
        self.pvals = pset[pset.keys()[0]]
        pvals = self.pvals
        fig = plt.figure(dpi=600, facecolor='w')
        cases = [i.case_number for i in self.cases]
        if len(self.slice_variables) == 1:
            fig = plt.figure(figsize=[6, 3], dpi=600, facecolor='w')
            ax1 = fig.add_axes([0.2, 0.3, 0.55, 0.6])
            ax2 = fig.add_axes([0.2, 0.2, 0.55, 0.075])
            ax3 = fig.add_axes([0.8, 0.2, 0.05, 0.7])
            xaxis = self.slice_variables[0]
            xvalues = [pset[i][xaxis] for i in pset]
            x_range = [min(xvalues)*1e-2, max(xvalues)*1e2]
            colors = ds.draw_1D_slice(ax2,
                                      pvals, xaxis, x_range, colorbar=False, intersections=[1, 3, 5, 7])
            ds.draw_1D_ss_function(ax1, self.y_variable,
                                   pvals, xaxis, x_range, colors=colors, lw=2.)
            for case_number in pset:
                case = controller.ds(case_number)
                pvals = pset[case_number]
                yval = case.ssystem.steady_state_function(self.y_variable, pvals)
                ax1.plot(np.log10(pset[case_number][xaxis]), yval, 
                         'o', mfc=colors[case_number], mec='k', ms=5., lw=2.)
            ax1.set_xticklabels('')
            ax1.set_xlabel('')
            ds.draw_region_colorbar(ax3, colors)
            title = 'System design space showing a 1-D case co-localization'
            caption = 'Different cases shown by line segments of different colors. '
            caption += 'Steady state function shown on the y-axis.  '
            caption += 'Bottom bar shows 1D design space slice showing valid cases '
            caption += 'as shown by the colorbar.'
            caption += ' Figure generated with the following parameter values: '
            caption += '; '.join([i + ' = ' + str(pvals[i]) for i in sorted(pvals) if i not in [xaxis]]) + '.'
        elif len(self.slice_variables) == 2:
            options = []
            fig = plt.figure(figsize=[6, 4], dpi=600, facecolor='w')
            ax = fig.add_axes([0.2, 0.2, 0.7, 0.7])
            xaxis = self.slice_variables[0]
            yaxis = self.slice_variables[1]
            xvalues = [pset[i][xaxis] for i in pset]
            yvalues = [pset[i][yaxis] for i in pset]
            x_range = [min(xvalues)*1e-2, max(xvalues)*1e2]
            y_range = [min(yvalues)*1e-2, max(yvalues)*1e2]
            colors = ds.draw_2D_slice(ax, pvals, xaxis, yaxis,
                                      x_range, y_range, included_cases=cases)
            for case_number in pset:
                case = controller.ds(case_number)
                pvals = pset[case_number]
                ax.plot(np.log10(pset[case_number][xaxis]), np.log10(pset[case_number][yaxis]), 
                         'o', mfc=colors[case_number], mec='k', ms=5., lw=2.)
            title = 'System design space showing a 2-D case co-localization'
            caption = 'Enumerated co-localized qualitatively-distinct phenotypes represented '
            caption += 'on the z-axis and identified by color.  '
            caption += 'Circles represent automatically determined values for each phenotype.'                
            caption += ' Figure generated with the following parameter values: '
            caption += '; '.join([i + ' = ' + str(pvals[i]) for i in sorted(pvals) if i not in [xaxis, yaxis]]) + '.'
        elif len(self.slice_variables) == 3:
            options = []
            fig = plt.figure(figsize=[6, 4], dpi=600, facecolor='w')
            ax = fig.add_axes([0.2, 0.2, 0.7, 0.7], projection='3d')
            xaxis = self.slice_variables[0]
            yaxis = self.slice_variables[1]
            zaxis = self.slice_variables[2]
            xvalues = [pset[i][xaxis] for i in pset]
            yvalues = [pset[i][yaxis] for i in pset]
            zvalues = [pset[i][zaxis] for i in pset]
            x_range = [min(xvalues)*1e-2, max(xvalues)*1e2]
            y_range = [min(yvalues)*1e-2, max(yvalues)*1e2]
            z_range = [min(zvalues)*1e-2, max(zvalues)*1e2]
            colors = ds.draw_3D_slice(ax, pvals, xaxis, yaxis, zaxis,
                                      x_range, y_range, z_range, included_cases=cases)
            title = 'System design space showing a 3-D case co-localization'
            caption = 'Enumerated co-localized qualitatively-distinct phenotypes represented '
            caption += 'on the z-axis and identified by color.  '
            caption += ' Figure generated with the following parameter values: '
            caption += '; '.join([i + ' = ' + str(pvals[i]) for i in sorted(pvals) if i not in [xaxis, yaxis]]) + '.'
        else:
            options = []
            slice_variables = self.slice_variables
            fig = plt.figure(figsize=[6, 3], dpi=600, facecolor='w')
            ax1 = fig.add_axes([0.2, 0.2, 0.55, 0.7])
            ax2 = fig.add_axes([0.8, 0.2, 0.05, 0.7])
            values = {i:[pset[i][axis] for axis in slice_variables] for i in pset}
            ## ranges = {axis:(min(values[axis])*1e-2,max(values[axis])*1e2) for axis in slice_variables}
            colors = {name:mt.cm.hsv(float(i)/float(len(cases))) for i, name in enumerate(cases)}
            min_value = None
            max_value = None
            for case in cases: 
                y_values = np.log10(values[case])
                min_y = min(y_values)
                max_y = max(y_values)
                if min_value is None:
                    min_value = min_y
                else:
                    min_value = min([min_value, min_y])
                if max_value is None:
                    max_value = max_y
                else:
                    max_value = max([max_value, max_y])
                ax1.plot(range(len(slice_variables)), 
                         y_values,
                         lw=2., c=colors[case])
            ax1.set_ylim([min_value-2, max_value+2])
            ax1.set_xticks(range(len(slice_variables)))
            ax1.set_xticklabels(['$' + controller.symbols[i] + '$' for i in slice_variables])
            ds.draw_region_colorbar(ax2, colors)
            ax1.set_xlim([0, len(slice_variables)-1])
            title = 'Values for the slice variable for the n-D case co-localization'
            caption = 'The y-axis represents value for the slice variable on the'
            caption += ' x-axis for a case identified by color.'
            caption += ' Figure generated with the following parameter values: '
            caption += '; '.join([i + ' = ' + str(pvals[i]) for i in sorted(pvals) if i not in slice_variables]) + '.'
        canvas = FigureCanvasAgg(fig) 
        plt.close()
        buf = cStringIO.StringIO()
        canvas.print_png(buf)
        data = buf.getvalue()
        controller.figures.add_figure(data, 
                                      title=title,
                                      caption=caption)
        
    
    def update_global_tolerances(self):
        controller = self.controller
        ds = controller.ds
        ci = self.ci
        if ci.is_valid() is False:
            self.global_tolerance.children = []
            return
        pvals = self.ci.valid_interior_parameter_set(project=False)
        if pvals is None:
            self.global_tolerance.children = []
            return
        table = HTML()
        html_str = '<div><table>\n<caption>Global tolerances determined for ' + self.name + ' showing fold-difference to a large qualitative change{0}. </caption>\n'.format(' in log-coordinates' if self.log_coordinates is True else '') 
        html_str += '<tr ><th align=center  rowspan=2 style="padding:0 15px 0 15px;"> Parameters </th><th colspan=2> Tolerance </th></tr>'
        html_str += '<tr><td style="padding:0 15px 0 15px;"><b> Lower bound</b></td><td style="padding:0 15px 0 15px;"><b> Upper bound</b></td></tr>'
        tolerances = ci.measure_tolerance(pvals, log_out=self.log_coordinates)
        for xi in sorted(pvals.keys()):
            lower_th = 1e-15 if self.log_coordinates is False else -15
            upper_th = 1e15 if self.log_coordinates is False else 15
            lower, upper = tolerances[xi]
            html_str += '<tr><td style="padding:0 15px 0 15px;"><b>{0}</b></td><td style="padding:0 15px 0 15px;">{1}</td><td style="padding:0 15px 0 15px;">{2}</td></tr>'.format(
                         xi,
                         lower if lower > lower_th else '-&infin;',
                         upper if upper < upper_th else '&infin;')
        html_str += '</table><caption>'
        html_str += 'Note: Global tolerance calculated based on the following values for the parameters: ' 
        html_str += '; '.join([i + ' = ' + str(pvals[i]) for i in sorted(pvals.keys())]) + '.'
        html_str += '</caption></div>'
        table.value = html_str
        save_button = Button(description='Save Global Tolerance Table')
        save_button.table_data = html_str
        save_button.on_click(self.save_table)
        self.global_tolerance.children = [HTML(value='<br>'),
                                          save_button,
                                          table]
        return
    
    def save_table(self, b):
        controller = self.controller
        html_string = b.table_data
        controller.tables.add_table(html_string)
        controller.save_widget_data(b)

    def change_y_axis(self, b):
        self.y_variable = str(b.yaxis.value)
        ## self.plot.children = [self.plot.children[0]]
        self.update_plot()
        
    def open_case(self, b):
        case_id = b.description.split(' ')[1]
        c_widget = DisplayCase(self.controller, 
                               str(case_id), 
                               by_signature=False,
                               pvals=b.pvals,
                               subtitle='Co-localized')
        c_widget.create_case_widget()

        
    def close_widget(self, b):
        controller = self.controller 
        controller.update_child(self.name, None)
class NotebookFitter(MPLFitter):
    __doc__ = _COMMON_DOC + """
    If IPython is available, it uses the IPython notebook's rich display
    to fit data interactively in a web-based GUI. The Parameters are
    represented in a web-based form that is kept in sync with `current_params`.
    All subclasses to Model, including user-defined ones, are shown in a
    drop-down menu.

    Clicking the "Fit" button updates a plot, as above, and updates the
    Parameters in the form to reflect the best fit.

    Parameters
    ----------
    data : array-like
    model : lmfit.Model
        optional initial Model to use, maybe be set or changed later
    all_models : list
        optional list of Models to populate drop-down menu, by default
        all built-in and user-defined subclasses of Model are used

    Additional Parameters
    ---------------------
    axes_style : dictionary representing style keyword arguments to be
        passed through to `Axes.set(...)`
    data_style : dictionary representing style keyword arguments to be passed
        through to the matplotlib `plot()` command the plots the data points
    init_style : dictionary representing style keyword arguments to be passed
        through to the matplotlib `plot()` command the plots the initial fit
        line
    best_style : dictionary representing style keyword arguments to be passed
        through to the matplotlib `plot()` command the plots the best fit
        line
    **kwargs : independent variables or extra arguments, passed like `x=x`
    """ + _COMMON_EXAMPLES_DOC
    def __init__(self, data, model=None, all_models=None, axes_style={},
                data_style={}, init_style={}, best_style={}, **kwargs):
        # Dropdown menu of all subclasses of Model, incl. user-defined.
        self.models_menu = Dropdown()
        # Dropbox API is very different between IPy 2.x and 3.x.
        if IPY2:
            if all_models is None:
                all_models = dict([(m.__name__, m) for m in Model.__subclasses__()])
            self.models_menu.values = all_models
        else:
            if all_models is None:
                all_models = [(m.__name__, m) for m in Model.__subclasses__()]
            self.models_menu.options = all_models
        self.models_menu.on_trait_change(self._on_model_value_change,
                                             'value')
        # Button to trigger fitting.
        self.fit_button = Button(description='Fit')
        self.fit_button.on_click(self._on_fit_button_click)

        # Button to trigger guessing.
        self.guess_button = Button(description='Auto-Guess')
        self.guess_button.on_click(self._on_guess_button_click)

        # Parameter widgets are not built here. They are (re-)built when
        # the model is (re-)set.
        super(NotebookFitter, self).__init__(data, model, axes_style,
                                             data_style, init_style,
                                             best_style, **kwargs)

    def _repr_html_(self):
        display(self.models_menu)
        button_box = HBox()
        button_box.children = [self.fit_button, self.guess_button]
        display(button_box)
        for pw in self.param_widgets:
            display(pw)
        self.plot()

    def guess(self):
        guessing_successful = super(NotebookFitter, self).guess()
        self.guess_button.disabled = not guessing_successful

    def _finalize_model(self, value):
        first_run = not hasattr(self, 'param_widgets')
        if not first_run:
            # Remove all Parameter widgets, and replace them with widgets
            # for the new model.
            for pw in self.param_widgets:
                pw.close()
        self.models_menu.value = value
        self.param_widgets = [ParameterWidgetGroup(p)
                              for _, p in self._current_params.items()]
        if not first_run:
            for pw in self.param_widgets:
                display(pw)

    def _finalize_params(self):
        for pw in self.param_widgets:
            pw.value = self._current_params[pw.name].value
            pw.min = self._current_params[pw.name].min
            pw.max = self._current_params[pw.name].max
            pw.vary = self._current_params[pw.name].vary

    def plot(self):
        clear_output(wait=True)
        super(NotebookFitter, self).plot()

    def fit(self):
        super(NotebookFitter, self).fit()
        self.plot()