def test_palette_context(self): """ Test the context manager for the color_palette function """ default_pal = color_palette() context_pal = color_palette("muted") with color_palette(context_pal): self.assertEqual(get_color_cycle(), context_pal) self.assertEqual(get_color_cycle(), default_pal)
def test_color_palette_context(self): """ Test ColorPalette context management """ default = color_palette() context = color_palette('dark') with ColorPalette('dark') as palette: self.assertIsInstance(palette, ColorPalette) self.assertEqual(get_color_cycle(), context) self.assertEqual(get_color_cycle(), default)
def test_color_palette_context(self): """ Test ColorPalette context management """ default = color_palette() context = color_palette("dark") with ColorPalette("dark") as palette: assert isinstance(palette, ColorPalette) assert get_color_cycle() == context assert get_color_cycle() == default
def test_palette_context(self): """ Test the context manager for the color_palette function """ default_pal = color_palette() context_pal = color_palette("muted") with color_palette(context_pal): self.assertEqual(get_color_cycle(), context_pal) self.assertEqual(get_color_cycle(), default_pal)
def test_color_palette_context(self): """ Test ColorPalette context management """ default = color_palette() context = color_palette('dark') with ColorPalette('dark') as palette: self.assertIsInstance(palette, ColorPalette) self.assertEqual(get_color_cycle(), context) self.assertEqual(get_color_cycle(), default)
def test_palette_context(self): """ Test the context manager for the color_palette function """ default_pal = color_palette() context_pal = color_palette("muted") with color_palette(context_pal): assert get_color_cycle() == context_pal assert get_color_cycle() == default_pal
def test_big_palette_context(self): """ Test that the context manager also resets the number of colors """ original_pal = color_palette("accent", n_colors=8) context_pal = color_palette("bold", 10) set_palette(original_pal) with color_palette(context_pal, 10): self.assertEqual(get_color_cycle(), context_pal) self.assertEqual(get_color_cycle(), original_pal) # Reset default set_aesthetic()
def test_big_palette_context(self): """ Test that the context manager also resets the number of colors """ original_pal = color_palette("accent", n_colors=8) context_pal = color_palette("bold", 10) set_palette(original_pal) with color_palette(context_pal, 10): self.assertEqual(get_color_cycle(), context_pal) self.assertEqual(get_color_cycle(), original_pal) # Reset default set_aesthetic()
def test_get_color_cycle(self): """ Test getting the default color cycle """ with warnings.catch_warnings(): warnings.simplefilter('ignore') result = get_color_cycle() expected = mpl.rcParams['axes.color_cycle'] self.assertEqual(result, expected)
def test_current_palette(self): """ Test modifying the current palette with a simple palette """ pal = color_palette(["red", "blue", "green"], 3) set_palette(pal, 3) self.assertEqual(pal, get_color_cycle()) # Reset the palette set_aesthetic()
def test_current_palette(self): """ Test modifying the current palette with a simple palette """ pal = color_palette(["red", "blue", "green"], 3) set_palette(pal, 3) self.assertEqual(pal, get_color_cycle()) # Reset the palette set_aesthetic()
def draw(self, X, y, **kwargs): """ Called from the fit method, this method creates the parallel coordinates canvas and draws each instance and vertical lines on it. """ # Get the shape of the data nrows, ncols = X.shape # Create the xticks for each column # TODO: Allow the user to specify this feature x = list(range(ncols)) # Create the axis if it doesn't exist if self.ax is None: self.ax = plt.gca() # Create the colors # TODO: Allow both colormap, listed colors, and palette definition # TODO: Make this an independent function or property for override! # color_values = resolve_colors( # num_colors=len(self.classes_), colormap=self.colormap, color=self.color # ) color_values = get_color_cycle() colors = dict(zip(self.classes_, color_values)) # Track which labels are already in the legend used_legends = set([]) # TODO: Make this function compatible with DataFrames! # TODO: Make an independent function to allow addition of instances! for idx, row in enumerate(X): # TODO: How to map classmap to labels? label = y[idx] # Get the label for the row label = self.classes_[label] if label not in used_legends: used_legends.add(label) self.ax.plot(x, row, color=colors[label], label=label, **kwargs) else: self.ax.plot(x, row, color=colors[label], **kwargs) # Add the vertical lines # TODO: Make an independent function for override! if self.show_vlines: for idx in x: self.ax.axvline(idx, **self.vlines_kwds) # Set the limits self.ax.set_xticks(x) self.ax.set_xticklabels(self.features_) self.ax.set_xlim(x[0], x[-1])
def draw(self, X, y, **kwargs): """ Called from the fit method, this method creates the parallel coordinates canvas and draws each instance and vertical lines on it. """ # Get the shape of the data nrows, ncols = X.shape # Create the xticks for each column # TODO: Allow the user to specify this feature x = list(range(ncols)) # Create the axis if it doesn't exist if self.ax is None: self.ax = plt.gca() # Create the colors # TODO: Allow both colormap, listed colors, and palette definition # TODO: Make this an independent function or property for override! # color_values = resolve_colors( # num_colors=len(self.classes_), colormap=self.colormap, color=self.color # ) color_values = get_color_cycle() colors = dict(zip(self.classes_, color_values)) # Track which labels are already in the legend used_legends = set([]) # TODO: Make this function compatible with DataFrames! # TODO: Make an independent function to allow addition of instances! for idx, row in enumerate(X): # TODO: How to map classmap to labels? label = y[idx] # Get the label for the row label = self.classes_[label] if label not in used_legends: used_legends.add(label) self.ax.plot(x, row, color=colors[label], label=label, **kwargs) else: self.ax.plot(x, row, color=colors[label], **kwargs) # Add the vertical lines # TODO: Make an independent function for override! if self.show_vlines: for idx in x: self.ax.axvline(idx, **self.vlines_kwds) # Set the limits self.ax.set_xticks(x) self.ax.set_xticklabels(self.features_) self.ax.set_xlim(x[0], x[-1])
def draw(self, X, y, **kwargs): """Called from the fit method, this method creates a scatter plot that draws each instance as a class or target colored point, whose location is determined by the feature data set. """ # Set the axes limits self.ax.set_xlim([-1,1]) self.ax.set_ylim([-1,1]) # set the colors if self.colormap is not None or self.color is not None: color_values = resolve_colors( num_colors=len(self.classes_), colormap=self.colormap, color=self.color) else: color_values = get_color_cycle() colors = dict(zip(self.classes_, color_values)) # Create a data structure to hold the scatter plot representations to_plot = {} for kls in self.classes_: to_plot[kls] = [[], []] # Add each row of the data set to to_plot for plotting # TODO: make this an independent function for override for i, row in enumerate(X): row_ = np.repeat(np.expand_dims(row, axis=1), 2, axis=1) x_, y_ = row_[0], row_[1] kls = self.classes_[y[i]] to_plot[kls][0].append(x_) to_plot[kls][1].append(y_) # Add the scatter plots from the to_plot function # TODO: store these plots to add more instances to later # TODO: make this a separate function for i, kls in enumerate(self.classes_): self.ax.scatter( to_plot[kls][0], to_plot[kls][1], marker=next(self.markers), color=colors[kls], label=str(kls), **kwargs) self.ax.axis('equal')
def draw(self, points, target=None, **kwargs): """ Called from the fit method, this method draws the TSNE scatter plot, from a set of decomposed points in 2 dimensions. This method also accepts a third dimension, target, which is used to specify the colors of each of the points. If the target is not specified, then the points are plotted as a single cloud to show similar documents. """ # Create the axis if it doesn't exist if self.ax is None: self.ax = plt.gca() # Create the color mapping for the classes. # TODO: Allow both colormap, listed colors, and palette definition # See the FeatureVisualizer for more on this. color_values = get_color_cycle() classes = self.classes_ or [None] colors = dict(zip(classes, color_values)) # Expand the points into vectors of x and y for scatter plotting, # assigning them to their label if the label has been passed in. # Additionally, filter classes not specified directly by the user. series = defaultdict(lambda: {'x': [], 'y': []}) if self.classes_: classes = frozenset(self.classes_) if target: for label, point in zip(target, points): if self.classes_ and label not in classes: continue series[label]['x'].append(point[0]) series[label]['y'].append(point[1]) else: for x, y in points: series[None]['x'].append(x) series[None]['y'].append(y) # Plot the points for label, points in series.items(): self.ax.scatter(points['x'], points['y'], c=colors[label], alpha=0.7, label=label)
def draw(self, X, y, **kwargs): """ Called from the fit method, this method creates the radviz canvas and draws each instance as a class or target colored point, whose location is determined by the feature data set. """ # Get the shape of the data nrows, ncols = X.shape # Create the axes if they don't exist if self.ax is None: self.ax = plt.gca(xlim=[-1,1], ylim=[-1,1]) # Create the colors # TODO: Allow both colormap, listed colors, and palette definition # TODO: Make this an independent function or property for override! # color_values = resolve_colors( # num_colors=len(self.classes_), colormap=self.colormap, color=self.color # ) color_values = get_color_cycle() colors = dict(zip(self.classes_, color_values)) # Create a data structure to hold scatter plot representations to_plot = {} for kls in self.classes_: to_plot[kls] = [[], []] # Compute the arcs around the circumference for each feature axis # TODO: make this an independent function for override s = np.array([ (np.cos(t), np.sin(t)) for t in [ 2.0 * np.pi * (i / float(ncols)) for i in range(ncols) ] ]) # Compute the locations of the scatter plot for each class # Normalize the data first to plot along the 0, 1 axis for i, row in enumerate(self.normalize(X)): row_ = np.repeat(np.expand_dims(row, axis=1), 2, axis=1) xy = (s * row_).sum(axis=0) / row.sum() kls = self.classes_[y[i]] to_plot[kls][0].append(xy[0]) to_plot[kls][1].append(xy[1]) # Add the scatter plots from the to_plot function # TODO: store these plots to add more instances to later # TODO: make this a separate function for i, kls in enumerate(self.classes_): self.ax.scatter(to_plot[kls][0], to_plot[kls][1], color=colors[kls], label=str(kls), **kwargs) # Add the circular axis path # TODO: Make this a seperate function (along with labeling) self.ax.add_patch(patches.Circle((0.0, 0.0), radius=1.0, facecolor='none')) # Add the feature names for xy, name in zip(s, self.features_): # Add the patch indicating the location of the axis self.ax.add_patch(patches.Circle(xy, radius=0.025, facecolor='#777777')) # Add the feature names offset around the axis marker if xy[0] < 0.0 and xy[1] < 0.0: self.ax.text(xy[0] - 0.025, xy[1] - 0.025, name, ha='right', va='top', size='small') elif xy[0] < 0.0 and xy[1] >= 0.0: self.ax.text(xy[0] - 0.025, xy[1] + 0.025, name, ha='right', va='bottom', size='small') elif xy[0] >= 0.0 and xy[1] < 0.0: self.ax.text(xy[0] + 0.025, xy[1] - 0.025, name, ha='left', va='top', size='small') elif xy[0] >= 0.0 and xy[1] >= 0.0: self.ax.text(xy[0] + 0.025, xy[1] + 0.025, name, ha='left', va='bottom', size='small') self.ax.axis('equal')
def draw(self, X, y, **kwargs): """ Called from the fit method, this method creates the parallel coordinates canvas and draws each instance and vertical lines on it. """ # Convert from dataframe if is_dataframe(X): X = X.as_matrix() # Choose a subset of samples # TODO: allow selection of a random subset of samples instead of head if isinstance(self.sample, int): self.n_samples = min([self.sample, len(X)]) elif isinstance(self.sample, float): self.n_samples = int(len(X) * self.sample) X = X[:self.n_samples, :] # Normalize if self.normalize is not None: X = self.normalizers[self.normalize].fit_transform(X) # Get the shape of the data nrows, ncols = X.shape # Create the xticks for each column # TODO: Allow the user to specify this feature x = list(range(ncols)) # Create the colors # TODO: Allow both colormap, listed colors, and palette definition # TODO: Make this an independent function or property for override! # color_values = resolve_colors( # num_colors=len(self.classes_), colormap=self.colormap, color=self.color # ) color_values = get_color_cycle() colors = dict(zip(self.classes_, color_values)) # Track which labels are already in the legend used_legends = set([]) # TODO: Make this function compatible with DataFrames! # TODO: Make an independent function to allow addition of instances! for idx, row in enumerate(X): # TODO: How to map classmap to labels? label = y[idx] # Get the label for the row label = self.classes_[label] if label not in used_legends: used_legends.add(label) self.ax.plot(x, row, color=colors[label], alpha=0.25, label=label, **kwargs) else: self.ax.plot(x, row, color=colors[label], alpha=0.25, **kwargs) # Add the vertical lines # TODO: Make an independent function for override! if self.show_vlines: for idx in x: self.ax.axvline(idx, **self.vlines_kwds) # Set the limits self.ax.set_xticks(x) self.ax.set_xticklabels(self.features_) self.ax.set_xlim(x[0], x[-1])
def draw(self, X, y, **kwargs): """ Called from the fit method, this method creates the parallel coordinates canvas and draws each instance and vertical lines on it. """ # Get the shape of the data nrows, ncols = X.shape # Create the axes if they don't exist if self.ax is None: self.ax = plt.gca(xlim=[-1, 1], ylim=[-1, 1]) # Create the colors # TODO: Allow both colormap, listed colors, and palette definition # TODO: Make this an independent function or property for override! # color_values = resolve_colors( # num_colors=len(self.classes_), colormap=self.colormap, color=self.color # ) color_values = get_color_cycle() colors = dict(zip(self.classes_, color_values)) # Create a data structure to hold scatter plot representations to_plot = {} for kls in self.classes_: to_plot[kls] = [[], []] # Compute the arcs around the circumference for each feature axis # TODO: make this an independent function for override s = np.array([ (np.cos(t), np.sin(t)) for t in [2.0 * np.pi * (i / float(ncols)) for i in range(ncols)] ]) # Compute the locations of the scatter plot for each class # Normalize the data first to plot along the 0, 1 axis for i, row in enumerate(self.normalize(X)): row_ = np.repeat(np.expand_dims(row, axis=1), 2, axis=1) xy = (s * row_).sum(axis=0) / row.sum() kls = self.classes_[y[i]] to_plot[kls][0].append(xy[0]) to_plot[kls][1].append(xy[1]) # Add the scatter plots from the to_plot function # TODO: store these plots to add more instances to later # TODO: make this a separate function for i, kls in enumerate(self.classes_): self.ax.scatter(to_plot[kls][0], to_plot[kls][1], color=colors[kls], label=str(kls), **kwargs) # Add the circular axis path # TODO: Make this a seperate function (along with labeling) self.ax.add_patch( patches.Circle((0.0, 0.0), radius=1.0, facecolor='none')) # Add the feature names for xy, name in zip(s, self.features_): # Add the patch indicating the location of the axis self.ax.add_patch( patches.Circle(xy, radius=0.025, facecolor='#777777')) # Add the feature names offset around the axis marker if xy[0] < 0.0 and xy[1] < 0.0: self.ax.text(xy[0] - 0.025, xy[1] - 0.025, name, ha='right', va='top', size='small') elif xy[0] < 0.0 and xy[1] >= 0.0: self.ax.text(xy[0] - 0.025, xy[1] + 0.025, name, ha='right', va='bottom', size='small') elif xy[0] >= 0.0 and xy[1] < 0.0: self.ax.text(xy[0] + 0.025, xy[1] - 0.025, name, ha='left', va='top', size='small') elif xy[0] >= 0.0 and xy[1] >= 0.0: self.ax.text(xy[0] + 0.025, xy[1] + 0.025, name, ha='left', va='bottom', size='small') self.ax.axis('equal')