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 reset_canvas(self, width, height): canvas_shape = pd.Series([width, height], index=['width', 'height']) if self.canvas is None or self.canvas.df_shapes.shape[0] == 0: self.canvas = ShapesCanvas(self.df_shapes, self.shape_i_columns, canvas_shape=canvas_shape, padding_fraction=self.padding_fraction) else: self.canvas.reset_shape(canvas_shape)