def make_2D_slice(ds=None, p_vals=None, x_variable=None, y_variable=None, range_x=None, range_y=None, intersections=None, image_widget=None, highlight='', **kwargs): for i in kwargs: p_vals[str(i)] = 10**kwargs[str(i)] x_range = range_x y_range = range_y fig = plt.Figure(figsize=[6, 4], dpi=600, facecolor='w') fig=plt.gcf() ax = fig.add_axes([0.2, 0.2, 0.7, 0.7]) ax = plt.gca() ds.draw_2D_slice(ax, p_vals, str(x_variable), str(y_variable), x_range, y_range, intersections=intersections) highlight = str(highlight) if highlight != '': try: case = ds(highlight) p_bounds = dict(p_vals) p_bounds[x_variable] = x_range p_bounds[y_variable] = y_range if case.is_valid(p_bounds=p_bounds, strict=False) is True: case.draw_2D_slice(ax, p_vals, str(x_variable), str(y_variable), x_range, y_range, fc='none', ec='k', lw='2.') except: pass ax.plot(log10(p_vals[x_variable]), log10(p_vals[y_variable]), 'k.') if image_widget is not None: fig = plt.gcf() canvas = FigureCanvasAgg(fig) buf = cStringIO.StringIO() canvas.print_png(buf) data = buf.getvalue() image_widget.value = data plt.close() return
def draw_2D_slice(self, ax, p_vals, x_variable, y_variable, range_x, range_y, color_dict=None, intersections=[1,2,3,4,5], included_cases=None, expand_cycles=True, colorbar=True, cmap=mt.cm.gist_rainbow, **kwargs): pvals = dspace.VariablePool(names=self.independent_variables) if set(pvals.keys()) != set(p_vals.keys()): raise ValueError, 'Incomplete parameter set' pvals.update(p_vals) p_vals = pvals p_bounds = dict(p_vals) p_bounds[x_variable] = range_x p_bounds[y_variable] = range_y hatched_cases = [] if included_cases is not None: included_cases = [i.case_number for i in self(included_cases)] if self.number_of_cases < 1e5: valid_cases = self.valid_cases(p_bounds=p_bounds) valid_nonstrict = self.valid_cases(p_bounds=p_bounds, strict=False) hatched_cases = [i for i in valid_cases if i not in included_cases] valid_nonstrict = [i for i in valid_cases if i in included_cases] valid_cases = [i for i in valid_cases if i in included_cases] valid_nonstrict = [i for i in valid_cases if i not in valid_cases] else: valid_cases = [i for i in included_cases if self(i).is_valid(p_bounds=p_bounds)] valid_nonstrict = [] else: valid_cases = self.valid_cases(p_bounds=p_bounds) valid_nonstrict = self.valid_cases(p_bounds=p_bounds, strict=False) valid_nonstrict = [i for i in valid_nonstrict if i not in valid_cases] if len(valid_cases)+len(valid_nonstrict) == 0: # fill black return case_int_list = self.intersecting_cases(intersections, valid_cases+valid_nonstrict, p_bounds=p_bounds, strict=False) colors = dict() if color_dict is None: color_dict = dict() if 'ec' not in kwargs: kwargs['ec']='none' ## hatched_cases = self.cycles_to_subcases(hatched_cases) for case_num in hatched_cases: case = self(case_num) case.draw_2D_slice(ax, p_vals, x_variable, y_variable, range_x, range_y, fc='none', ec=(0.8, 0.8, 0.8, 1.), hatch='/', lw=0.5) for case_int in case_int_list: key = str(case_int) case_nums = key.split(', ') for i in xrange(len(case_nums)): if expand_cycles is False: case_nums[i] = str(case_nums[i]).split('_')[0] if case_nums[i] in valid_nonstrict: case_nums[i] = str(case_nums[i])+'*' key = ', '.join(case_nums) if key not in color_dict: color_dict[key] = cmap((1.*case_int_list.index(case_int))/len(case_int_list)) V = case_int.draw_2D_slice(ax, p_vals, x_variable, y_variable, range_x, range_y, fc=color_dict[key], **kwargs) colors[key] = color_dict[key] ax.set_xlim([log10(min(range_x)), log10(max(range_x))]) ax.set_ylim([log10(min(range_y)), log10(max(range_y))]) if x_variable in self._latex: x_variable = '$'+self._latex[x_variable]+'$' if y_variable in self._latex: y_variable = '$'+self._latex[y_variable]+'$' ax.set_xlabel(r'$\log_{10}$(' + x_variable + ')') ax.set_ylabel(r'$\log_{10}$(' + y_variable + ')') if colorbar is False: return color_dict if colorbar is True or colorbar == 'auto': labels = colors.keys() try: labels.sort(cmp=key_sort_function) except: pass labels.reverse() num = 0 while num < len(labels): temp_dict = {i:colors[i] for i in labels[num:min(num+20, len(labels))]} c_ax,kw=mt.colorbar.make_axes(ax) c_ax.set_aspect(15) self.draw_region_colorbar(c_ax, temp_dict) num += 20 plt.sca(ax) elif colorbar == 'single': labels = colors.keys() try: labels.sort(cmp=key_sort_function) except: pass labels.reverse() c_ax,kw=mt.colorbar.make_axes(ax) c_ax.set_aspect(15) self.draw_region_colorbar(c_ax, colors) plt.sca(ax) return color_dict