def __init__(self, svg_filepath, name=None, **kwargs): self.name = name or path(svg_filepath).namebase # Read SVG paths and polygons from `Device` layer into data frame, one # row per polygon vertex. self.df_shapes = svg_shapes_to_df(svg_filepath, xpath=ELECTRODES_XPATH) # Add SVG file path as attribute. self.svg_filepath = svg_filepath self.shape_i_columns = 'id' # Create temporary shapes canvas with same scale as original shapes # frame. This canvas is used for to conduct point queries to detect # which shape (if any) overlaps with the endpoint of a connection line. svg_canvas = ShapesCanvas(self.df_shapes, self.shape_i_columns) # Detect connected shapes based on lines in "Connection" layer of the # SVG. self.df_shape_connections = extract_connections(self.svg_filepath, svg_canvas) # Scale coordinates to millimeter units. self.df_shapes[['x', 'y']] -= self.df_shapes[['x', 'y']].min().values self.df_shapes[['x', 'y']] /= INKSCAPE_PPmm.magnitude self.df_shapes = compute_shape_centers(self.df_shapes, self.shape_i_columns) self.df_electrode_channels = self.get_electrode_channels() self.graph = nx.Graph() for index, row in self.df_shape_connections.iterrows(): self.graph.add_edge(row['source'], row['target']) # Get data frame, one row per electrode, indexed by electrode path id, # each row denotes electrode center coordinates. self.df_shape_centers = (self.df_shapes.drop_duplicates(subset=['id']) .set_index('id')[['x_center', 'y_center']]) (self.adjacency_matrix, self.indexed_shapes, self.shape_indexes) = get_adjacency_matrix(self.df_shape_connections) self.df_indexed_shape_centers = (self.df_shape_centers .loc[self.shape_indexes.index] .reset_index()) self.df_indexed_shape_centers.rename(columns={'index': 'shape_id'}, inplace=True) self.df_shape_connections_indexed = self.df_shape_connections.copy() self.df_shape_connections_indexed['source'] = \ map(str, self.shape_indexes[self.df_shape_connections['source']]) self.df_shape_connections_indexed['target'] \ = map(str, self.shape_indexes[self.df_shape_connections ['target']]) self.df_shapes_indexed = self.df_shapes.copy() self.df_shapes_indexed['id'] = map(str, self.shape_indexes [self.df_shapes['id']]) # Modified state (`True` if electrode channels have been updated). self._dirty = False
def __init__(self, svg_source, **kwargs): from svg_model import svg_polygons_to_df, compute_shape_centers extend = kwargs.pop('extend', .5) self.electrode_channels = svg_polygons_to_channels(svg_source, **kwargs) # Read device layout from SVG file. df_device = svg_polygons_to_df(svg_source) #self.df_paths = scale_svg_frame(df_device) self.df_paths = compute_shape_centers(df_device, 'path_id') self.df_connected = extract_adjacent_paths(self.df_paths, extend) self.df_connected['cost'] = 1 # The following returns one row per electrode, indexed by electrode path id self.df_path_centers = (self.df_paths.drop_duplicates(['path_id']) .set_index('path_id')[['x_center', 'y_center']]) (self.adjacency_matrix, self.indexed_paths, self.path_indexes) = get_adjacency_matrix(self.df_connected) self.df_indexed_path_centers = (self.df_path_centers .loc[self.path_indexes.index] .reset_index()) self.df_indexed_path_centers.rename(columns={'index': 'path_id'}, inplace=True) self.df_connected_indexed = self.df_connected.copy() self.df_connected_indexed['source'] = map(str, self.path_indexes [self.df_connected ['source']]) self.df_connected_indexed['target'] = map(str, self.path_indexes [self.df_connected ['target']]) self.df_paths_indexed = self.df_paths.copy() self.df_paths_indexed['path_id'] = map(str, self.path_indexes [self.df_paths.path_id]) self.graph = nx.Graph() for index, row in self.df_connected.iterrows(): self.graph.add_edge(row['source'], row['target'], cost=row['cost'])
def reset_canvas(self, width, height): super(DmfDeviceCanvas, self).reset_canvas(width, height) if self.device is None or self.canvas.df_canvas_shapes.shape[0] == 0: return self.canvas.df_canvas_shapes =\ compute_shape_centers(self.canvas.df_canvas_shapes [[self.shape_i_column, 'vertex_i', 'x', 'y']], self.shape_i_column) self.canvas.df_shape_centers = (self.canvas.df_canvas_shapes [[self.shape_i_column, 'x_center', 'y_center']].drop_duplicates() .set_index(self.shape_i_column)) df_shape_connections = self.device.df_shape_connections self.canvas.df_connection_centers =\ (df_shape_connections.join(self.canvas.df_shape_centers .loc[df_shape_connections.source] .reset_index(drop=True)) .join(self.canvas.df_shape_centers.loc[df_shape_connections .target] .reset_index(drop=True), lsuffix='_source', rsuffix='_target'))