Ejemplo n.º 1
0
  def view(self):
    """ The view method will display the control panel to select the FCS path
    and the arcsin factor.
    """
    self.widgets.fcs_dir.guess_or_remember('load_fcs fcs_dir', '')
    dirname = self.widgets.fcs_dir.value_as_str()
    if os.path.exists(dirname) and os.path.isdir(dirname):
      files = [f for f in os.listdir(dirname) if os.path.isfile(os.path.join(dirname, f))]
    else:
      return stack_lines(
          View(self, 'Please select a valid directory'),
          self.widgets.fcs_dir.view('FCS Directory', self.widgets.apply, numeric_validation=False, size=100),
          self.widgets.apply.view())

    # Clear file list if we switched directory:
    if dirname != self.last_selected_dir:
      self.widgets.fcs_files.values.choices = None
      self.last_selected_dir = dirname

    self.widgets.fcs_files.guess_or_remember(('load_fcs fcs_files', files, dirname), [])
    self.widgets.combine_select.guess_or_remember(('load_fcs combine_select', dirname), ['combine'])
    self.widgets.arcsin_factor.guess_or_remember(('load_fcs arcsin_factor', dirname), 5)

    # display the control panel:
    return stack_lines(
        self.widgets.fcs_dir.view('FCS Directory', self.widgets.apply, numeric_validation=False, size=100),
        self.widgets.fcs_files.view('FCS Files', self.widgets.apply, files),
        self.widgets.combine_select.view('Combine files', self.widgets.apply, [('combine', 'Combine files into one table'), ('seperate', 'Seperate to a table list')], multiple=False),
        self.widgets.arcsin_factor.view('Arcsinh Factor', self.widgets.apply, numeric_validation=False, comment='Transformation: arcsinh(value / factor)', predefined_values=[('No Transform', 'NO_TRANSFORM')]),
        self.widgets.apply.view())
Ejemplo n.º 2
0
 def view(self, apply_button, datatable):
   experiments = settings.EXPERIMENTS.keys()
   if not self.experiment:
     return stack_lines(
         self.widgets.experiment_select.view('Please Select an experiment', self.widgets.apply, experiments, False),
         self.widgets.apply.view())
   
   if not self.experiment in self.experiment_to_widgets:
     index = self._get_index()
     widgets = []
     if type(settings.EXPERIMENTS[self.experiment]) == tuple:
       editable_tags = settings.EXPERIMENTS[self.experiment][1]
     else:
       editable_tags = index.all_tags()
     for tag in editable_tags:
       new_widget = self._add_widget('%s_%s' % (self.experiment, tag), Select)
       new_widget.tag = tag
       new_widget.vals = index.all_values_for_tag(tag)        
       widgets.append(new_widget)
     self.experiment_to_widgets[self.experiment] = widgets
   
   views = [w.view(w.tag, self.widgets.apply, w.vals) for w in self.experiment_to_widgets[self.experiment]]
   stacked_views = stack_left(*views)
     
   expanded = stack_lines(
       self.widgets.experiment_select.view(
           'Experiment', self.widgets.apply, experiments, False),
       stacked_views,
       View(None, '<p style="clear: both"></p>'),
       self.widgets.apply.view())
   #return expanded
   return self.widgets.expander.view('',[self.summary], [expanded])
Ejemplo n.º 3
0
  def view(self, data, place_in_chain, possible_inputs):
    def create_inputs_summary():
      summary = []
      for input in self.input_to_select:
        choices = self.input_to_select[input].values.choices
        choice_texts = [p[1] for p in possible_inputs if p[0] in choices]
        #summary.append('%s: %s' % (input, choice_text))
        summary.append('|'.join(choice_texts))
      ret = ', '.join(summary)  
      if ret:
        return 'Inputs: %s ' % ret
      else:
        if self.input_to_select:
          return 'Inputs'
        else:
          return ''

    #input_summary = View(self, '%sOutputs: %s' % (
    #    create_inputs_summary(),
    #    ', '.join(self.get_outputs())))
    
    input_summary = View(self, create_inputs_summary())

    input_content_views = []
    for k,v in self.input_to_select.items():
       input_content_views.append(v.view(
           k, self.widgets.input_apply, possible_inputs, multiple=True))
    input_content_views.append(self.widgets.input_apply.view())
    input_content = view.stack_lines(*input_content_views)    
    try:
      sub_widget_view = self.widgets.sub_widget.view(**self.create_input_map(data))
    except Exception as e:
      logging.exception('Exception in view')
      sub_widget_view = View(self, str(e))

    global CHAINABLE_WIDGETS
    widget_control_view = stack_left(
        self.widgets.new_widget_select.view('', self.widgets.apply_new_widget, zip(*CHAINABLE_WIDGETS)[0], False),
        self.widgets.apply_new_widget.view('Add before'),
        self.widgets.delete_button.view('Delete'))
    
    widget_control_view.main_html = '<div style="position:absolute; top:0; right:0;">%s</div>' % widget_control_view.main_html

    sub_view = view.stack_lines(
        widget_control_view,
        self.widgets.input_panel.view(input_summary, input_content), 
        view.vertical_seperator(),
        sub_widget_view)
    

    #sub_view = self.widgets.sub_widget.view(data)  
    #title_view = view.left_right(
    #    View(self, self.title(place_in_chain)), 
    #    self.widgets.delete_button.view('Delete'))
    title_view = View(self, self.title(place_in_chain))
    return self.widgets.expander.view(title_view, sub_view)
Ejemplo n.º 4
0
    def main_view(self, tables):
        if 'all' in self.widgets.tables_to_show.get_choices():
            tables_to_show = tables
        else:
            tables_to_show = [
                t for t in tables
                if t.name in self.widgets.tables_to_show.get_choices()
            ]
        if not tables_to_show:
            return
        # Assemble the figure to show
        figures_to_show = []
        multi_timer = MultiTimer(len(tables_to_show))
        for table_input in tables_to_show:
            figures_to_show.append(
                self._draw_figures(table_input,
                                   self.widgets.dim_x.get_choices(),
                                   self.widgets.dim_y.get_choices(),
                                   tables_to_show))
            multi_timer.complete_task(table_input.name)

        # Draw the figures in a table
        lines = []
        for key in figures_to_show[0].iterkeys():
            line = []
            for i in xrange(len(tables_to_show)):
                fig_widget = self._add_widget_if_needed(
                    '%d_%s' % (i, key), Figure)
                fig, extra_view, fig_range = figures_to_show[i][key]
                # We give the view function a predefined id so that we can reference this viewed instance from
                # the areaselect widget in gating mode.
                widget_id = self._get_unique_id()
                if fig:
                    fig_view = fig_widget.view(fig, id=widget_id)
                    if self.enable_gating:
                        area_select_widget = self._add_widget_if_needed(
                            'area_select_%s' % widget_id, AreaSelect)
                        area_select_view = area_select_widget.view(
                            widget_id, self.gate_min_x_id, self.gate_max_x_id,
                            self.gate_min_y_id, self.gate_max_y_id, fig_range)
                        fig_view = stack_lines(fig_view, area_select_view)
                else:
                    fig_view = View(self, '')
                # On gating mode we want to add areaselect.
                line.append(stack_lines(fig_view, extra_view))
            lines.append(line)
        if len(tables_to_show) == 1:
            return view.stack_left(*[l[0] for l in lines])
        else:
            self._add_widget_if_needed('input_table', Table)
            return self.widgets.input_table.view(
                [table_input.name for table_input in tables_to_show], lines)
Ejemplo n.º 5
0
  def view(self, table, dim_x, dim_y):
    splitted = table.split(dim_x, 100j)
    stats = [s.get_stats_multi_dim(dim_x, dim_y) for s in splitted if s.num_cells>=25]
    if len(stats) < 10:
      return View(None, 'Not enough cells in each bin')

    joined = combine_tables(stats)
    ax = new_axes(300, 300)
    ax, hist = axes.scatter(ax, table, (dim_x, dim_y), norm_axis=None, no_bins=300j)
       
    x_points = np.linspace(0,10,300)
    avg_points = joined.get_cols(dim_x+'_average', dim_y + '_average')
    std_points = joined.get_cols(dim_x+'_average', dim_y + '_std')
    
    std_fit, std_res, std_rank, std_s, std_r = np.polyfit(std_points[0], std_points[1], 1, full=True)
    avg_fit, avg_res, avg_rank, avg_s, avg_r = np.polyfit(avg_points[0], avg_points[1], 1, full=True)
    avg_sig_fit = sigmoid_fit(avg_points[0], avg_points[1])
    sig_error = find_error(avg_points[0], avg_points[1], sigmoid, avg_sig_fit)

    ax.plot(avg_points[0], avg_points[1], '.')
    #ax.plot(x_points, np.poly1d(avg_fit)(x_points), '-')
    ax.plot(x_points, sigmoid(avg_sig_fit, x_points), '-')
    #ax.scatter(avg_points[0], avg_points[1], s=1, marker='o', color='blue')
    ax.scatter(std_points[0], std_points[1], s=1, marker='o')
    
    ax.set_xlabel(dim_x + '   ', size='x-small')
    ax.set_ylabel(dim_y + '   ', size='x-small')
    ax.set_xlim(0,8)
    ax.set_ylim(0,8)
    
    
    return stack_lines(
        self.widgets.fig.view(ax.figure),
        str(avg_sig_fit),
        str(sig_error))
Ejemplo n.º 6
0
 def view(self, tables):
   global NETWORKS
   self.widgets.network.guess_or_remember((tables, 'Network'), [])
   self.widgets.edge_stat.guess_or_remember((tables, 'Network'), [])
   self.widgets.node_stat.guess_or_remember((tables, 'Network'), [])
     
   control_panel_view = stack_lines(
     self.widgets.network.view('Network', self.widgets.apply, sorted(NETWORKS.keys())),
     self.widgets.edge_stat.view('Edge statistic to show', self.widgets.apply, sorted(EDGE_STATS.keys())),
     self.widgets.node_stat.view('Node statistic to show', self.widgets.apply, sorted(NODE_STATS.keys())),
     self.widgets.apply.view())
   
   networks = [n for n in self.widgets.network.values.choices if n in NETWORKS]
   main_views = []
   for network in networks:
     nodes = set()
     edges = set()
     for graph_entry in NETWORKS[network]:
       nodes.add((graph_entry[0], node_label(graph_entry[0], tables, self.widgets.node_stat.values.choices), 0))
       nodes.add((graph_entry[1], node_label(graph_entry[1], tables, self.widgets.node_stat.values.choices), 0))
       edges.add((edge_label(graph_entry[0], graph_entry[1], tables, self.widgets.edge_stat.values.choices), 0, graph_entry[0], graph_entry[1], True))
     main_views += [self._get_widget('graph_%s' % network).view(nodes, edges)]
     
   main_view = stack_left(*main_views)
   return self.widgets.layout.view(main_view, control_panel_view)
Ejemplo n.º 7
0
 def view(self):
   if not self.widgets.population_picker.is_ready():
     return self.widgets.population_picker.view()
     
   if not self.widgets.negative_values_picker.values.choices:
     self.widgets.negative_values_picker.values.choices = ['remove']
   
   table = self.widgets.population_picker.get_data()
   
   return view.stack_lines(
       self.widgets.population_picker.view(),
       self.widgets.dim_picker.view(
           'Dimension',
           self.widgets.apply,
           table.dims, True, [
               ('Signal Markers', get_markers('signal')),
               ('Surface Markers', get_markers('surface'))]),
       self.widgets.negative_values_picker.view(
           'Negative Values',
           self.widgets.apply,
           [('remove', 'Remove negative values'),
            ('keep', 'Keep negative values')], False),
       self.widgets.apply.view(),
       self.widgets.histogram_table.view(
           table,
           self.widgets.dim_picker.values.choices,
           'remove' in self.widgets.negative_values_picker.values.choices))
Ejemplo n.º 8
0
  def view(self, **tables):
    self.widgets.edge_stat.guess_or_remember((tables, 'Compare'), [])
    self.widgets.node_stat.guess_or_remember((tables, 'Compare'), [])
    
    NODE_STATS = ['values']
    EDGE_STATS = []

    control_panel_view = stack_lines(
      self.widgets.edge_stat.view('Edge statistics', self.widgets.apply, sorted(EDGE_STATS)),
      self.widgets.node_stat.view('Node statistics', self.widgets.apply, sorted(NODE_STATS)),
      self.widgets.apply.view())
    
    
    data_tables = [t for t in tables.values() if t]
    networks = [n for n in self.widgets.network.values.choices if n in NETWORKS]
    main_views = []
    for network in networks:
      nodes = set()
      edges = set()
      for graph_entry in NETWORKS[network]:
        nodes.add((graph_entry[0], node_label(graph_entry[0], data_tables, self.widgets.node_stat.values.choices), 0))
        nodes.add((graph_entry[1], node_label(graph_entry[1], data_tables, self.widgets.node_stat.values.choices), 0))
        edges.add((edge_label(graph_entry[0], graph_entry[1], data_tables, self.widgets.edge_stat.values.choices), 0, graph_entry[0], graph_entry[1], True))
      main_views += [self._get_widget('graph_%s' % network).view(nodes, edges)]
      
    main_view = stack_left(*main_views)
    return self.widgets.layout.view(main_view, control_panel_view)
Ejemplo n.º 9
0
    def view(self):
        global CHAINABLE_WIDGETS
        # Run the chain:
        data = []
        views = []
        possible_inputs = []
        for i, widget in enumerate(self.widgets_in_chain):
            #logging.info('Getting view for Widget %d %s' % (i, widget.title(i, True)))
            with Timer('Module %d view' % (i + 1)):
                views.append(widget.view(data, i, possible_inputs))
            possible_inputs += self.widget_in_chain_to_inputs(widget, i)
            #logging.info('Running Widget %d %s' % (i, widget.title(i)))
            try:
                with Timer('Module %d run' % (i + 1)):
                    widget_data = widget.run(data)
                data.append(widget_data)
                views.append(self.output_log(widget_data))
                if widget_data and 'view' in widget_data:
                    views.append(widget_data['view'])
                    del widget_data['view']
                if widget_data:
                    for item in widget_data.keys():
                        from biology.datatable import DataTable
                        if type(item) == list:
                            for sub_item in item:
                                if type(sub_item
                                        ) == DataTable and not item.name:
                                    raise Exception(
                                        'DataTable outputs must have a name')
            except Exception as e:
                logging.exception('Exception in run')
                views.append('Exception when running %s: %s' %
                             (widget.title(i), str(e)))
                break
        del data

        widgets_view = stack_lines(*views)
        add_view = stack(
            self.widgets.new_widget_select.view('Add new module',
                                                self.widgets.apply_new_widget,
                                                zip(*CHAINABLE_WIDGETS)[0],
                                                False),
            self.widgets.apply_new_widget.view())
        return stack_lines(self.widgets.menu.view(self.parent.id),
                           widgets_view, view.vertical_seperator(), add_view)
Ejemplo n.º 10
0
  def view(self, enable_expander=False):
    experiments = settings.EXPERIMENTS.keys()
    if not self.experiment:
      return stack_lines(
          self.widgets.experiment_select.view('Please Select an experiment', self.widgets.apply, experiments, False),
          self.widgets.apply.view())
    
    if not self.experiment in self.experiment_to_widgets:
      index = self._get_index()
      widgets = []
      if type(settings.EXPERIMENTS[self.experiment]) == tuple:
        editable_tags = settings.EXPERIMENTS[self.experiment][1]
      else:
        editable_tags = index.all_tags()
      for tag in editable_tags:
        new_widget = self._add_widget('%s_%s' % (self.experiment, tag), Select)
        new_widget.tag = tag
        new_widget.vals = index.all_values_for_tag(tag)        
        widgets.append(new_widget)
      self.experiment_to_widgets[self.experiment] = widgets
    
    for w in self.experiment_to_widgets[self.experiment]:
      w.guess_or_remember((w.tag, w.vals, self.__class__.__name__), [])
    
    self.widgets.combine_select.guess_or_remember(('populationpicker combine_select', self.experiment), ['combine'])
    self.widgets.arcsin_factor.guess_or_remember(('populationpicker arcsin_factor', self.experiment), 5)
    
    #if not self.widgets.arcsin_factor.values.value:
    #  self.widgets.arcsin_factor.values.value = '1'

    views = [w.view(w.tag, self.widgets.apply, w.vals) for w in self.experiment_to_widgets[self.experiment]]
    stacked_views = stack_lines(*views)
 
    expanded = stack_lines(
        self.widgets.experiment_select.view(
            'Experiment', self.widgets.apply, experiments, False),
        stacked_views,
        self.widgets.combine_select.view('Combine files', self.widgets.apply, [('combine', 'Combine items into one table'), ('seperate', 'Seperate to one table per parameter combination')], multiple=False),
        self.widgets.arcsin_factor.view('Arcsinh Factor', self.widgets.apply, numeric_validation=True, comment='Transformation: arcsinh(value / factor)'),
        View(None, '<p style="clear: both"></p>'),
        self.widgets.apply.view())
    if not enable_expander:
      return expanded
    return self.widgets.expander.view('',[self.summary], [expanded])
Ejemplo n.º 11
0
 def view(self):
   global CHAINABLE_WIDGETS
   # Run the chain:
   data = []
   views = []
   possible_inputs = []
   for i, widget in enumerate(self.widgets_in_chain):
     #logging.info('Getting view for Widget %d %s' % (i, widget.title(i, True)))
     with Timer('Module %d view' % (i+1)):
       views.append(widget.view(data, i, possible_inputs))
     possible_inputs += self.widget_in_chain_to_inputs(widget, i)
     #logging.info('Running Widget %d %s' % (i, widget.title(i)))
     try:
       with Timer('Module %d run' % (i+1)):
         widget_data = widget.run(data)
       data.append(widget_data)
       views.append(self.output_log(widget_data))
       if widget_data and 'view' in widget_data:
         views.append(widget_data['view'])
         del widget_data['view']
       if widget_data:
         for item in widget_data.keys():
           from biology.datatable import DataTable
           if type(item) == list:
             for sub_item in item:
               if type(sub_item) == DataTable and not item.name:
                 raise Exception('DataTable outputs must have a name')
     except Exception as e:
       logging.exception('Exception in run')
       views.append('Exception when running %s: %s' % (widget.title(i), str(e)))
       break
   del data
   
   widgets_view = stack_lines(*views)
   add_view = stack(
       self.widgets.new_widget_select.view(
           'Add new module', self.widgets.apply_new_widget, zip(*CHAINABLE_WIDGETS)[0], False),
       self.widgets.apply_new_widget.view())
   return stack_lines(
       self.widgets.menu.view(self.parent.id),
       widgets_view,
       view.vertical_seperator(),
       add_view)
Ejemplo n.º 12
0
  def main_view(self, tables):
    if 'all' in self.widgets.tables_to_show.get_choices():
      tables_to_show = tables
    else:
      tables_to_show = [t for t in tables if t.name in self.widgets.tables_to_show.get_choices()]
    if not tables_to_show:
      return
    # Assemble the figure to show
    figures_to_show = []
    multi_timer = MultiTimer(len(tables_to_show))
    for table_input in tables_to_show:
      figures_to_show.append(self._draw_figures(table_input, self.widgets.dim_x.get_choices(), self.widgets.dim_y.get_choices(), tables_to_show))
      multi_timer.complete_task(table_input.name)

    # Draw the figures in a table
    lines = []
    for key in figures_to_show[0].iterkeys():
      line = []
      for i in xrange(len(tables_to_show)):
        fig_widget = self._add_widget_if_needed('%d_%s' % (i, key), Figure)
        fig, extra_view, fig_range = figures_to_show[i][key]      
        # We give the view function a predefined id so that we can reference this viewed instance from
        # the areaselect widget in gating mode.  
        widget_id = self._get_unique_id()
        if fig:
          fig_view = fig_widget.view(fig, id=widget_id)
          if self.enable_gating:
            area_select_widget = self._add_widget_if_needed('area_select_%s' % widget_id, AreaSelect)
            area_select_view = area_select_widget.view(
                widget_id, self.gate_min_x_id, self.gate_max_x_id, self.gate_min_y_id, self.gate_max_y_id, fig_range)
            fig_view = stack_lines(fig_view, area_select_view)          
        else:
          fig_view = View(self, '')
        # On gating mode we want to add areaselect.
        line.append(stack_lines(fig_view, extra_view))
      lines.append(line)  
    if len(tables_to_show) == 1:
      return view.stack_left(*[l[0] for l in lines])
    else:
      self._add_widget_if_needed('input_table', Table)
      return self.widgets.input_table.view([table_input.name for table_input in tables_to_show], lines)
 def _end_section(self, section_name, initially_expanded=True):
   """ Ends a section, must be called after _begin_section.
   """
   if not section_name in self._section_begin:
     raise Exception('begin section %s must called before ending section' % section_name)
   section_begin = self._section_begin[section_name]
   widgets_in_section = self._control_panel_views[section_begin:]
   self._control_panel_views = self._control_panel_views[:section_begin]
   widget = self._add_widget_if_needed(section_name, MiniExpander, shown=initially_expanded)
   view = widget.view(View(self, section_name), stack_lines(*widgets_in_section))
   self._control_panel_views.append(view)
   del self._section_begin[section_name]
Ejemplo n.º 14
0
  def view_pair(self, p, table_positive):
      fig1 = new_figure(300,300)
      axes.kde2d_color_hist(fig1, table_positive, (p[0], p[1]), None, None, 0.001)
      fig = new_figure(300,300)
      axes.kde2d_color_hist(fig, table_positive, (p[0], p[1]), None, 'y', 0.001)       
      #ax_dis = new_axes(300,300)
      dis_points = find_discontinuities(table_positive, p[0], p[1], None)
      return stack_lines(
          self.widgets.kde2d_fig.view(fig1),
          self.widgets.kde2d_fig.view(fig),
#          self.widgets.kde2d_fig.view(ax_dis.figure),
          dis_points,
          self.widgets.pair_fit_split.view(table_positive, p[0], p[1]))
Ejemplo n.º 15
0
  def main_view(self, tables):
    dims = self.widgets.dims.get_choices()
    if 'all' in self.widgets.tables_to_show.get_choices():
      tables_to_show = tables
    else:
      tables_to_show = [t for t in tables if t.name in self.widgets.tables_to_show.get_choices()]
    if not tables_to_show:
      return
    # Assemble the figure to show
    figures_to_show = []
    multi_timer = MultiTimer(len(tables_to_show))
    for table in tables_to_show:
       
      comments = []
      col_names = []
      cols = []
    
      # Add id column
      col_names.append('id')
      cols.append(dims)

      # Add time column
      col_names.append('time')
      cols.append([0] * len(dims))
    
      # Add dim reduce columns
      col_names.append('dim_reduce1')
      col_names.append('dim_reduce2')
      corr = self.widgets.corr_method.get_choice() == 'corr'
      mutual_information_table = table.get_mutual_information_table(dims_to_use = dims, use_correlation=corr)      
      #assert np.all(mutual_information_table.data >= 0)
      distance = 1-np.abs(mutual_information_table.data)
      from mlabwrap import mlab
      Y, eig = mlab.cmdscale(distance, nout=2)
      cols.append(Y.T[0])
      cols.append(Y.T[1])
      comments.append(','.join(['%.3f' % val for val in eig[:4]]))
    
      # Add average columns:
      col_names.append('average')
      cols.append([table.get_average(dim) for dim in dims])

      # Add std columns:
      col_names.append('std')
      cols.append([table.get_std(dim) for dim in dims])
      
      motion_chart = self._add_widget_if_needed('motion_chart_%s' % table.name, MotionChart)
      motion_chart.guess_or_remember(('multicompare motionchart', tables), None)
      figures_to_show.append(motion_chart.view(col_names, zip(*cols), [''], '\n'.join(comments)))
    return stack_lines(*figures_to_show)
Ejemplo n.º 16
0
    def view(self):
        experiments = settings.EXPERIMENTS.keys()
        if not self.experiment:
            return stack_lines(
                self.widgets.experiment_select.view(
                    'Please Select an experiment', self.widgets.apply,
                    experiments, False), self.widgets.apply.view())

        if not self.experiment in self.experiment_to_widgets:
            index = self._get_index()
            widgets = []
            if type(settings.EXPERIMENTS[self.experiment]) == tuple:
                editable_tags = settings.EXPERIMENTS[self.experiment][1]
            else:
                editable_tags = index.all_tags()
            for tag in editable_tags:
                new_widget = self._add_widget('%s_%s' % (self.experiment, tag),
                                              Select)
                new_widget.tag = tag
                new_widget.vals = index.all_values_for_tag(tag)
                widgets.append(new_widget)
            self.experiment_to_widgets[self.experiment] = widgets

        views = [
            w.view(w.tag, self.widgets.apply, w.vals)
            for w in self.experiment_to_widgets[self.experiment]
        ]
        stacked_views = stack_left(*views)

        expanded = stack_lines(
            self.widgets.experiment_select.view('Experiment',
                                                self.widgets.apply,
                                                experiments, False),
            stacked_views, View(None, '<p style="clear: both"></p>'),
            self.widgets.apply.view())
        #return expanded
        return self.widgets.expander.view('', [self.summary], [expanded])
Ejemplo n.º 17
0
 def view(self, table, dim_x, dim_y):
     #ax_dis = new_axes(300,300)
     dis_points = find_discontinuities(table, dim_x, dim_y, None)
     splits = []
     split_start = np.min(table.get_points(dim_y))
     dis_points = np.r_[dis_points, np.max(table.get_points(dim_y))]
     min_split_fraction = 0.1      
     for p in dis_points:
       split = table.gate(DimRange(dim_y, split_start, p))
       if split.num_cells >= min_split_fraction * table.num_cells:
         splits.append(split)
         split_start = p
     
     return stack_lines(
         *[self.widgets.pair.view(s, dim_x, dim_y) for s in splits])
Ejemplo n.º 18
0
    def view(self, table, dim_x, dim_y):
        #ax_dis = new_axes(300,300)
        dis_points = find_discontinuities(table, dim_x, dim_y, None)
        splits = []
        split_start = np.min(table.get_points(dim_y))
        dis_points = np.r_[dis_points, np.max(table.get_points(dim_y))]
        min_split_fraction = 0.1
        for p in dis_points:
            split = table.gate(DimRange(dim_y, split_start, p))
            if split.num_cells >= min_split_fraction * table.num_cells:
                splits.append(split)
                split_start = p

        return stack_lines(
            *[self.widgets.pair.view(s, dim_x, dim_y) for s in splits])
Ejemplo n.º 19
0
 def view_pair(self, p, table_positive):
     fig1 = new_figure(300, 300)
     axes.kde2d_color_hist(fig1, table_positive, (p[0], p[1]), None, None,
                           0.001)
     fig = new_figure(300, 300)
     axes.kde2d_color_hist(fig, table_positive, (p[0], p[1]), None, 'y',
                           0.001)
     #ax_dis = new_axes(300,300)
     dis_points = find_discontinuities(table_positive, p[0], p[1], None)
     return stack_lines(
         self.widgets.kde2d_fig.view(fig1),
         self.widgets.kde2d_fig.view(fig),
         #          self.widgets.kde2d_fig.view(ax_dis.figure),
         dis_points,
         self.widgets.pair_fit_split.view(table_positive, p[0], p[1]))
Ejemplo n.º 20
0
 def view(self):
     index = DataIndex.load(r'c:\cytof54_clustered\cytof54.index')
     t = index.load_table(cluster_name='CD8+ T-cells', stim='Unstim1')
     #t = index.load_table(cluster_name='B-cells CD20+', stim='Unstim1')
     dim_x = '152-Ki67'
     dim_y = '167-CD38'
     t = t.remove_bad_cells(dim_x, dim_y)
     from histogram_report import FitTable
     splitted = t.split(dim_x, 100j)
     views = []
     for s in splitted:
         views.append(View(self, '<h1>%s</h1>' % s.name))
         views.append(View(self, '<h1>%d cells</h1>' % s.num_cells))
         views.append(FitTable().view(s, [dim_y]))
     views = [PairTable().view([[dim_x, dim_y]], t)] + views
     return stack_lines(*views)
Ejemplo n.º 21
0
 def view(self):
   index = DataIndex.load(r'c:\cytof54_clustered\cytof54.index')
   t = index.load_table(cluster_name='CD8+ T-cells', stim='Unstim1')
   #t = index.load_table(cluster_name='B-cells CD20+', stim='Unstim1')
   dim_x = '152-Ki67'
   dim_y = '167-CD38'
   t = t.remove_bad_cells(dim_x, dim_y)
   from histogram_report import FitTable
   splitted = t.split(dim_x, 100j)
   views = []
   for s in splitted:
     views.append(View(self, '<h1>%s</h1>' % s.name))
     views.append(View(self, '<h1>%d cells</h1>' % s.num_cells))     
     views.append(FitTable().view(s, [dim_y]))
   views = [PairTable().view([[dim_x, dim_y]], t)] + views
   return stack_lines(*views)
Ejemplo n.º 22
0
    def view(self, table, dim_x, dim_y):
        splitted = table.split(dim_x, 100j)
        stats = [
            s.get_stats_multi_dim(dim_x, dim_y) for s in splitted
            if s.num_cells >= 25
        ]
        if len(stats) < 10:
            return View(None, 'Not enough cells in each bin')

        joined = combine_tables(stats)
        ax = new_axes(300, 300)
        ax, hist = axes.scatter(ax,
                                table, (dim_x, dim_y),
                                norm_axis=None,
                                no_bins=300j)

        x_points = np.linspace(0, 10, 300)
        avg_points = joined.get_cols(dim_x + '_average', dim_y + '_average')
        std_points = joined.get_cols(dim_x + '_average', dim_y + '_std')

        std_fit, std_res, std_rank, std_s, std_r = np.polyfit(std_points[0],
                                                              std_points[1],
                                                              1,
                                                              full=True)
        avg_fit, avg_res, avg_rank, avg_s, avg_r = np.polyfit(avg_points[0],
                                                              avg_points[1],
                                                              1,
                                                              full=True)
        avg_sig_fit = sigmoid_fit(avg_points[0], avg_points[1])
        sig_error = find_error(avg_points[0], avg_points[1], sigmoid,
                               avg_sig_fit)

        ax.plot(avg_points[0], avg_points[1], '.')
        #ax.plot(x_points, np.poly1d(avg_fit)(x_points), '-')
        ax.plot(x_points, sigmoid(avg_sig_fit, x_points), '-')
        #ax.scatter(avg_points[0], avg_points[1], s=1, marker='o', color='blue')
        ax.scatter(std_points[0], std_points[1], s=1, marker='o')

        ax.set_xlabel(dim_x + '   ', size='x-small')
        ax.set_ylabel(dim_y + '   ', size='x-small')
        ax.set_xlim(0, 8)
        ax.set_ylim(0, 8)

        return stack_lines(self.widgets.fig.view(ax.figure), str(avg_sig_fit),
                           str(sig_error))
Ejemplo n.º 23
0
    def view(self, tables):
        global NETWORKS
        self.widgets.network.guess_or_remember((tables, 'Network'), [])
        self.widgets.edge_stat.guess_or_remember((tables, 'Network'), [])
        self.widgets.node_stat.guess_or_remember((tables, 'Network'), [])

        control_panel_view = stack_lines(
            self.widgets.network.view('Network', self.widgets.apply,
                                      sorted(NETWORKS.keys())),
            self.widgets.edge_stat.view('Edge statistic to show',
                                        self.widgets.apply,
                                        sorted(EDGE_STATS.keys())),
            self.widgets.node_stat.view('Node statistic to show',
                                        self.widgets.apply,
                                        sorted(NODE_STATS.keys())),
            self.widgets.apply.view())

        networks = [
            n for n in self.widgets.network.values.choices if n in NETWORKS
        ]
        main_views = []
        for network in networks:
            nodes = set()
            edges = set()
            for graph_entry in NETWORKS[network]:
                nodes.add(
                    (graph_entry[0],
                     node_label(graph_entry[0], tables,
                                self.widgets.node_stat.values.choices), 0))
                nodes.add(
                    (graph_entry[1],
                     node_label(graph_entry[1], tables,
                                self.widgets.node_stat.values.choices), 0))
                edges.add((edge_label(graph_entry[0], graph_entry[1], tables,
                                      self.widgets.edge_stat.values.choices),
                           0, graph_entry[0], graph_entry[1], True))
            main_views += [
                self._get_widget('graph_%s' % network).view(nodes, edges)
            ]

        main_view = stack_left(*main_views)
        return self.widgets.layout.view(main_view, control_panel_view)
Ejemplo n.º 24
0
    def view(self, **tables):
        self.widgets.edge_stat.guess_or_remember((tables, 'Compare'), [])
        self.widgets.node_stat.guess_or_remember((tables, 'Compare'), [])

        NODE_STATS = ['values']
        EDGE_STATS = []

        control_panel_view = stack_lines(
            self.widgets.edge_stat.view('Edge statistics', self.widgets.apply,
                                        sorted(EDGE_STATS)),
            self.widgets.node_stat.view('Node statistics', self.widgets.apply,
                                        sorted(NODE_STATS)),
            self.widgets.apply.view())

        data_tables = [t for t in tables.values() if t]
        networks = [
            n for n in self.widgets.network.values.choices if n in NETWORKS
        ]
        main_views = []
        for network in networks:
            nodes = set()
            edges = set()
            for graph_entry in NETWORKS[network]:
                nodes.add(
                    (graph_entry[0],
                     node_label(graph_entry[0], data_tables,
                                self.widgets.node_stat.values.choices), 0))
                nodes.add(
                    (graph_entry[1],
                     node_label(graph_entry[1], data_tables,
                                self.widgets.node_stat.values.choices), 0))
                edges.add(
                    (edge_label(graph_entry[0], graph_entry[1], data_tables,
                                self.widgets.edge_stat.values.choices), 0,
                     graph_entry[0], graph_entry[1], True))
            main_views += [
                self._get_widget('graph_%s' % network).view(nodes, edges)
            ]

        main_view = stack_left(*main_views)
        return self.widgets.layout.view(main_view, control_panel_view)
 def view(self, *args, **kargs):
   self._add_widget_if_needed('layout', LeftPanel)
   self._add_widget_if_needed('control_panel_apply', ApplyButton)
   
   if not any(list(args) + list(kargs.values())):
     return View(self, 'No tables to display')
   self._control_panel_views = []  
   extra_control_panel_view = self.control_panel(*args, **kargs)
   if extra_control_panel_view:
     self._control_panel_views.append(extra_control_panel_view)
   self._control_panel_views.append(self.widgets.control_panel_apply.view())
   if not self.widgets.control_panel_apply.clicked_once:
     main_view = View(self, 'Click on \'apply\' to run this module')
   else:
     try:
       main_view = self.main_view(*args, **kargs)
       if not main_view:
         main_view = View(self, '')
     except Exception as e:
       logging.exception('Exception in main_view')
       main_view = View(self, str(e))
   return self.widgets.layout.view(main_view, stack_lines(*self._control_panel_views))
Ejemplo n.º 26
0
  def view(self):
    if self.widgets.population_picker.is_ready() and self.widgets.population_picker.get_data(True) > 2000:
      t = self.widgets.population_picker.get_data()
      #t = fake_table((1,0.1), (20,1))

      samples_percent = min((10000. / t.num_cells) * 100, 100)
      num_samples = int((samples_percent/100) * t.data.shape[0])
      truncate_cells_mi = True
      t_samp = t.random_sample(num_samples)
    
      t_mi = t_samp.get_mutual_information(
          t.get_markers('signal'),
          ignore_negative_values=truncate_cells_mi)
      table = self.widgets.pair_table.view(all_pairs_with_mi_sorted(t_mi)[:15], t, t_mi)
    elif self.widgets.population_picker.is_ready():
      table = 'Not enougth cells -- need at least 2000'
    else:
      table = ''
    
    return stack_lines(
        self.widgets.population_picker.view(),
        table)
Ejemplo n.º 27
0
    def view(self):
        if self.widgets.population_picker.is_ready(
        ) and self.widgets.population_picker.get_data(True) > 2000:
            t = self.widgets.population_picker.get_data()
            #t = fake_table((1,0.1), (20,1))

            samples_percent = min((10000. / t.num_cells) * 100, 100)
            num_samples = int((samples_percent / 100) * t.data.shape[0])
            truncate_cells_mi = True
            t_samp = t.random_sample(num_samples)

            t_mi = t_samp.get_mutual_information(
                t.get_markers('signal'),
                ignore_negative_values=truncate_cells_mi)
            table = self.widgets.pair_table.view(
                all_pairs_with_mi_sorted(t_mi)[:15], t, t_mi)
        elif self.widgets.population_picker.is_ready():
            table = 'Not enougth cells -- need at least 2000'
        else:
            table = ''

        return stack_lines(self.widgets.population_picker.view(), table)
Ejemplo n.º 28
0
    def view(self):
        if not self.widgets.population_picker.is_ready():
            return self.widgets.population_picker.view()

        if not self.widgets.negative_values_picker.values.choices:
            self.widgets.negative_values_picker.values.choices = ['remove']

        table = self.widgets.population_picker.get_data()

        return view.stack_lines(
            self.widgets.population_picker.view(),
            self.widgets.dim_picker.view(
                'Dimension', self.widgets.apply, table.dims, True,
                [('Signal Markers', get_markers('signal')),
                 ('Surface Markers', get_markers('surface'))]),
            self.widgets.negative_values_picker.view(
                'Negative Values', self.widgets.apply,
                [('remove', 'Remove negative values'),
                 ('keep', 'Keep negative values')], False),
            self.widgets.apply.view(),
            self.widgets.histogram_table.view(
                table, self.widgets.dim_picker.values.choices, 'remove'
                in self.widgets.negative_values_picker.values.choices))
Ejemplo n.º 29
0
    def view(self, data, place_in_chain, possible_inputs):
        def create_inputs_summary():
            summary = []
            for input in self.input_to_select:
                choices = self.input_to_select[input].values.choices
                choice_texts = [
                    p[1] for p in possible_inputs if p[0] in choices
                ]
                #summary.append('%s: %s' % (input, choice_text))
                summary.append('|'.join(choice_texts))
            ret = ', '.join(summary)
            if ret:
                return 'Inputs: %s ' % ret
            else:
                if self.input_to_select:
                    return 'Inputs'
                else:
                    return ''

        #input_summary = View(self, '%sOutputs: %s' % (
        #    create_inputs_summary(),
        #    ', '.join(self.get_outputs())))

        input_summary = View(self, create_inputs_summary())

        input_content_views = []
        for k, v in self.input_to_select.items():
            input_content_views.append(
                v.view(k,
                       self.widgets.input_apply,
                       possible_inputs,
                       multiple=True))
        input_content_views.append(self.widgets.input_apply.view())
        input_content = view.stack_lines(*input_content_views)
        try:
            sub_widget_view = self.widgets.sub_widget.view(
                **self.create_input_map(data))
        except Exception as e:
            logging.exception('Exception in view')
            sub_widget_view = View(self, str(e))

        global CHAINABLE_WIDGETS
        widget_control_view = stack_left(
            self.widgets.new_widget_select.view('',
                                                self.widgets.apply_new_widget,
                                                zip(*CHAINABLE_WIDGETS)[0],
                                                False),
            self.widgets.apply_new_widget.view('Add before'),
            self.widgets.delete_button.view('Delete'))

        widget_control_view.main_html = '<div style="position:absolute; top:0; right:0;">%s</div>' % widget_control_view.main_html

        sub_view = view.stack_lines(
            widget_control_view,
            self.widgets.input_panel.view(input_summary, input_content),
            view.vertical_seperator(), sub_widget_view)

        #sub_view = self.widgets.sub_widget.view(data)
        #title_view = view.left_right(
        #    View(self, self.title(place_in_chain)),
        #    self.widgets.delete_button.view('Delete'))
        title_view = View(self, self.title(place_in_chain))
        return self.widgets.expander.view(title_view, sub_view)
Ejemplo n.º 30
0
    def view(self, enable_expander=False):
        experiments = settings.EXPERIMENTS.keys()
        if not self.experiment:
            return stack_lines(
                self.widgets.experiment_select.view(
                    'Please Select an experiment', self.widgets.apply,
                    experiments, False), self.widgets.apply.view())

        if not self.experiment in self.experiment_to_widgets:
            index = self._get_index()
            widgets = []
            if type(settings.EXPERIMENTS[self.experiment]) == tuple:
                editable_tags = settings.EXPERIMENTS[self.experiment][1]
            else:
                editable_tags = index.all_tags()
            for tag in editable_tags:
                new_widget = self._add_widget('%s_%s' % (self.experiment, tag),
                                              Select)
                new_widget.tag = tag
                new_widget.vals = index.all_values_for_tag(tag)
                widgets.append(new_widget)
            self.experiment_to_widgets[self.experiment] = widgets

        for w in self.experiment_to_widgets[self.experiment]:
            w.guess_or_remember((w.tag, w.vals, self.__class__.__name__), [])

        self.widgets.combine_select.guess_or_remember(
            ('populationpicker combine_select', self.experiment), ['combine'])
        self.widgets.arcsin_factor.guess_or_remember(
            ('populationpicker arcsin_factor', self.experiment), 5)

        #if not self.widgets.arcsin_factor.values.value:
        #  self.widgets.arcsin_factor.values.value = '1'

        views = [
            w.view(w.tag, self.widgets.apply, w.vals)
            for w in self.experiment_to_widgets[self.experiment]
        ]
        stacked_views = stack_lines(*views)

        expanded = stack_lines(
            self.widgets.experiment_select.view('Experiment',
                                                self.widgets.apply,
                                                experiments, False),
            stacked_views,
            self.widgets.combine_select.view(
                'Combine files',
                self.widgets.apply,
                [('combine', 'Combine items into one table'),
                 ('seperate',
                  'Seperate to one table per parameter combination')],
                multiple=False),
            self.widgets.arcsin_factor.view(
                'Arcsinh Factor',
                self.widgets.apply,
                numeric_validation=True,
                comment='Transformation: arcsinh(value / factor)'),
            View(None,
                 '<p style="clear: both"></p>'), self.widgets.apply.view())
        if not enable_expander:
            return expanded
        return self.widgets.expander.view('', [self.summary], [expanded])
Ejemplo n.º 31
0
  def view(self, tables):
    """ The view method of this module draws the control panel and the histograms. 
    We need at least one input to be able to draw something.
    """
    if not tables:
      return View(self, 'No tables to show.')
    self.widgets.color.guess_or_remember(('histogram text', tables), ['name'])
    self.widgets.text.guess_or_remember(('histogram colors', tables), ['name'])
    self.widgets.shift.guess_or_remember(('histogram shift', tables), '0.2')
    self.widgets.sort_inside.guess_or_remember(('histogram sort inside', tables), ['similarity'])
    self.widgets.sort_outside.guess_or_remember(('histogram sort outside', tables), ['sort'])
    self.widgets.trim.guess_or_remember(('histogram trim', tables), ['no'])
    self.widgets.trim_thresh.guess_or_remember(('histogram trim thresh', tables), '0')

    sort_inside_options = [('unsort', 'Keep original order'), ('similarity', 'Put similar curves together')]
    sort_inside_options += [(x, 'Sort by %s' % x) for x in tables[0].tags.keys()]
    
    # Create the control panel view. This will enable users to choose the dimensions. 
    control_panel_view = stack_lines(
        self.widgets.dims.view('Dimension', self.widgets.apply, options_from_table(tables[0])),
        self.widgets.text.view('Text by', self.widgets.apply, tables[0].tags.keys()),
        self.widgets.color.view('Color by', self.widgets.apply, tables[0].tags.keys()),
        self.widgets.shift.view('Shift for multiple curves', self.widgets.apply),
        self.widgets.sort_inside.view('Curve sorting', self.widgets.apply, 
                                      sort_inside_options,
                                      multiple=False),
        self.widgets.sort_outside.view('Plot sorting', self.widgets.apply, 
                                      [('sort', 'Put plots with many differences first'), ('unsort', 'Keep original order')],
                                      multiple=False),
        self.widgets.trim.view('Trim plots', self.widgets.apply,
            [('yes', 'Convert values lower than threshold to 0'), ('no', 'Don\'t trim')], multiple=False),
        self.widgets.trim_thresh.view('Trim threshold', self.widgets.apply),
        self.widgets.apply.view())
    main_views = []
    shift = self.widgets.shift.value_as_float()
    plots_for_legend = OrderedDict()
    colorer = axes.Colorer()
    # Check that the user has already chosen dimensions. Otherwise, ask him 
    # to do so.
    if self.widgets.dims.values.choices:
      timer = MultiTimer(len(self.widgets.dims.values.choices))
      for i, dim in enumerate(self.widgets.dims.values.choices):
        try:
          # Go over every dimension and create the histogram:
          # First create a new figure:
          fig = self.create_and_adjust_figure(tables)
          ax = fig.add_subplot(111)
          
          # Draw the histogram for every input
          plots = []
          sorted_tables = tables
          sort_method = self.widgets.sort_inside.values.choices[0]
          if sort_method == 'unsort':
            sorted_tables = tables
          elif sort_method == 'similarity':
            thresh = None
            if self.widgets.trim.get_choices()[0] == 'yes':
              thresh = self.widgets.trim_thresh.value_as_float()
            # get distances table:
            distances = datatable.ks_distances(tables, dim, thresh)
            # sort by distance
            sorted_tables = greedy_distance_sort(distances, tables)
          else:
            # we need to sort by tags:
            tag_for_sort = self.widgets.sort_inside.values.choices[0]
            sorted_tables = sorted(tables, key=lambda table: table.tags[tag_for_sort])
          for i, table in enumerate(sorted_tables):
            color_tags = self.widgets.color.values.choices
            color_key = tuple([table.tags[c] for c in color_tags])
            min_x = None
            if self.widgets.trim.get_choices()[0] =='yes':
              min_x = self.widgets.trim_thresh.value_as_float()
            plot = axes.kde1d(ax, table, dim,
                              color=colorer.get_color(color_key),
                              min_x=min_x,
                              shift=shift*i)
            plots_for_legend[color_key] = plot
          # Add ticks with table names:
          if self.widgets.shift.value_as_float() > 0:
            ax.set_yticks(np.arange(0, len(tables)*shift, shift))
            ax.set_yticklabels([t.get_tags(self.widgets.text.values.choices) for t in sorted_tables], size='xx-small')
          # set axes y range:
          ax.set_ylim(bottom = -0.1, top=0.8+shift*(len(sorted_tables)-1))
          # Make sure we don't create the same widget twice. We create a new widget
          # for every dimension asked. 
          widget_key = self._normalize_id(dim)
          if not widget_key in self.widgets:
            self._add_widget(widget_key, Figure)
          figure_widget = self.widgets[widget_key]
        
          if len(tables) > 1:
            from scipy.stats import ks_2samp
            ks, p_ks = ks_2samp(tables[0].get_cols(dim)[0], tables[1].get_cols(dim)[0])
            ks_view = View(self, 'ks: %.3f, p_ks: %.10f' % (ks, p_ks))
            final_view = stack_lines(ks_view, figure_widget.view(fig))
          else:
            ks, p_ks = 0, 0
            final_view = figure_widget.view(fig)
          # Add the new widget's view
          main_views.append((ks, p_ks, final_view))
        except Exception as e:
          logging.exception('Exception when drawing histogram')
          main_views.append((0, 0, View(self, str(e))))
                  
        timer.complete_task(dim)
      
      # sort by the ks test:
      main_views = sorted(main_views, key=itemgetter(0), reverse=True)
      main_views = [v[2] for v in main_views]
      
      
      
      
      # create legend:
      legened_titles = plots_for_legend.keys()
      print len(legened_titles)
      max_title_len = max([len(str(t)) for t in legened_titles] + [0])
      print max_title_len
      WIDTH_PER_LETTER = 7
      EXTRA_WIDTH = 60
      HEIGHT_PER_LINE = 30
      EXTRA_HEIGHT = 50
      MIN_X = 300
      MIN_Y = 100
      legend_x = max(MIN_X, EXTRA_WIDTH + WIDTH_PER_LETTER * max_title_len)
      legend_y = max(MIN_Y, EXTRA_HEIGHT + HEIGHT_PER_LINE * len(legened_titles))
      fig = axes.new_figure(legend_x, legend_y)
      ax = fig.add_subplot(111)
      ax.get_xaxis().set_visible(False)
      ax.get_yaxis().set_visible(False)
      ax.legend(plots_for_legend.values(),
                plots_for_legend.keys(),
                loc='center',
                mode='expand',
                frameon=False,
                prop={'size' : 'xx-small'})
      main_views = [self.widgets.legend_figure.view(fig)] + main_views
      main_view = view.stack_left(*main_views)
      
      
    else:
      main_view = View(None, 'Please select dimensions')    
    # combine the control panel and the main view togeteher:
    return self.widgets.layout.view(main_view, control_panel_view)
Ejemplo n.º 32
0
    def view(self, tables):
        """ The view method of this module draws the control panel and the histograms. 
    We need at least one input to be able to draw something.
    """
        if not tables:
            return View(self, 'No tables to show.')
        self.widgets.color.guess_or_remember(('histogram text', tables),
                                             ['name'])
        self.widgets.text.guess_or_remember(('histogram colors', tables),
                                            ['name'])
        self.widgets.shift.guess_or_remember(('histogram shift', tables),
                                             '0.2')
        self.widgets.sort_inside.guess_or_remember(
            ('histogram sort inside', tables), ['similarity'])
        self.widgets.sort_outside.guess_or_remember(
            ('histogram sort outside', tables), ['sort'])
        self.widgets.trim.guess_or_remember(('histogram trim', tables), ['no'])
        self.widgets.trim_thresh.guess_or_remember(
            ('histogram trim thresh', tables), '0')

        sort_inside_options = [('unsort', 'Keep original order'),
                               ('similarity', 'Put similar curves together')]
        sort_inside_options += [(x, 'Sort by %s' % x)
                                for x in tables[0].tags.keys()]

        # Create the control panel view. This will enable users to choose the dimensions.
        control_panel_view = stack_lines(
            self.widgets.dims.view('Dimension', self.widgets.apply,
                                   options_from_table(tables[0])),
            self.widgets.text.view('Text by', self.widgets.apply,
                                   tables[0].tags.keys()),
            self.widgets.color.view('Color by', self.widgets.apply,
                                    tables[0].tags.keys()),
            self.widgets.shift.view('Shift for multiple curves',
                                    self.widgets.apply),
            self.widgets.sort_inside.view('Curve sorting',
                                          self.widgets.apply,
                                          sort_inside_options,
                                          multiple=False),
            self.widgets.sort_outside.view(
                'Plot sorting',
                self.widgets.apply,
                [('sort', 'Put plots with many differences first'),
                 ('unsort', 'Keep original order')],
                multiple=False),
            self.widgets.trim.view(
                'Trim plots',
                self.widgets.apply,
                [('yes', 'Convert values lower than threshold to 0'),
                 ('no', 'Don\'t trim')],
                multiple=False),
            self.widgets.trim_thresh.view('Trim threshold',
                                          self.widgets.apply),
            self.widgets.apply.view())
        main_views = []
        shift = self.widgets.shift.value_as_float()
        plots_for_legend = OrderedDict()
        colorer = axes.Colorer()
        # Check that the user has already chosen dimensions. Otherwise, ask him
        # to do so.
        if self.widgets.dims.values.choices:
            timer = MultiTimer(len(self.widgets.dims.values.choices))
            for i, dim in enumerate(self.widgets.dims.values.choices):
                try:
                    # Go over every dimension and create the histogram:
                    # First create a new figure:
                    fig = self.create_and_adjust_figure(tables)
                    ax = fig.add_subplot(111)

                    # Draw the histogram for every input
                    plots = []
                    sorted_tables = tables
                    sort_method = self.widgets.sort_inside.values.choices[0]
                    if sort_method == 'unsort':
                        sorted_tables = tables
                    elif sort_method == 'similarity':
                        thresh = None
                        if self.widgets.trim.get_choices()[0] == 'yes':
                            thresh = self.widgets.trim_thresh.value_as_float()
                        # get distances table:
                        distances = datatable.ks_distances(tables, dim, thresh)
                        # sort by distance
                        sorted_tables = greedy_distance_sort(distances, tables)
                    else:
                        # we need to sort by tags:
                        tag_for_sort = self.widgets.sort_inside.values.choices[
                            0]
                        sorted_tables = sorted(
                            tables, key=lambda table: table.tags[tag_for_sort])
                    for i, table in enumerate(sorted_tables):
                        color_tags = self.widgets.color.values.choices
                        color_key = tuple([table.tags[c] for c in color_tags])
                        min_x = None
                        if self.widgets.trim.get_choices()[0] == 'yes':
                            min_x = self.widgets.trim_thresh.value_as_float()
                        plot = axes.kde1d(ax,
                                          table,
                                          dim,
                                          color=colorer.get_color(color_key),
                                          min_x=min_x,
                                          shift=shift * i)
                        plots_for_legend[color_key] = plot
                    # Add ticks with table names:
                    if self.widgets.shift.value_as_float() > 0:
                        ax.set_yticks(np.arange(0, len(tables) * shift, shift))
                        ax.set_yticklabels([
                            t.get_tags(self.widgets.text.values.choices)
                            for t in sorted_tables
                        ],
                                           size='xx-small')
                    # set axes y range:
                    ax.set_ylim(bottom=-0.1,
                                top=0.8 + shift * (len(sorted_tables) - 1))
                    # Make sure we don't create the same widget twice. We create a new widget
                    # for every dimension asked.
                    widget_key = self._normalize_id(dim)
                    if not widget_key in self.widgets:
                        self._add_widget(widget_key, Figure)
                    figure_widget = self.widgets[widget_key]

                    if len(tables) > 1:
                        from scipy.stats import ks_2samp
                        ks, p_ks = ks_2samp(tables[0].get_cols(dim)[0],
                                            tables[1].get_cols(dim)[0])
                        ks_view = View(self,
                                       'ks: %.3f, p_ks: %.10f' % (ks, p_ks))
                        final_view = stack_lines(ks_view,
                                                 figure_widget.view(fig))
                    else:
                        ks, p_ks = 0, 0
                        final_view = figure_widget.view(fig)
                    # Add the new widget's view
                    main_views.append((ks, p_ks, final_view))
                except Exception as e:
                    logging.exception('Exception when drawing histogram')
                    main_views.append((0, 0, View(self, str(e))))

                timer.complete_task(dim)

            # sort by the ks test:
            main_views = sorted(main_views, key=itemgetter(0), reverse=True)
            main_views = [v[2] for v in main_views]

            # create legend:
            legened_titles = plots_for_legend.keys()
            print len(legened_titles)
            max_title_len = max([len(str(t)) for t in legened_titles] + [0])
            print max_title_len
            WIDTH_PER_LETTER = 7
            EXTRA_WIDTH = 60
            HEIGHT_PER_LINE = 30
            EXTRA_HEIGHT = 50
            MIN_X = 300
            MIN_Y = 100
            legend_x = max(MIN_X,
                           EXTRA_WIDTH + WIDTH_PER_LETTER * max_title_len)
            legend_y = max(
                MIN_Y, EXTRA_HEIGHT + HEIGHT_PER_LINE * len(legened_titles))
            fig = axes.new_figure(legend_x, legend_y)
            ax = fig.add_subplot(111)
            ax.get_xaxis().set_visible(False)
            ax.get_yaxis().set_visible(False)
            ax.legend(plots_for_legend.values(),
                      plots_for_legend.keys(),
                      loc='center',
                      mode='expand',
                      frameon=False,
                      prop={'size': 'xx-small'})
            main_views = [self.widgets.legend_figure.view(fig)] + main_views
            main_view = view.stack_left(*main_views)

        else:
            main_view = View(None, 'Please select dimensions')
        # combine the control panel and the main view togeteher:
        return self.widgets.layout.view(main_view, control_panel_view)