def dot(self, filename=None, stage=None): """Generate directed graph using graphviz notation""" def fmt_dec(value): return ('%.2f' % value).rstrip('0').rstrip('.') if filename is None: filename = tmpfilename('.png') self.dot(filename=filename, stage=stage) display_matplotlib(filename) return base, ext = path.splitext(filename) if ext in ('.pdf', '.png'): dotfilename = filename + '.dot' self.dot(dotfilename, stage=stage) run_dot(dotfilename, filename) return if stage != 0: self.analyse(stage=stage) dotfile = open(filename, 'w') dotfile.write('strict digraph {\n\tgraph [rankdir=LR];\n') for gnode in self.values(): if hasattr(gnode, 'fixed'): colour = 'yellow' else: colour = 'red' if gnode.pos is not None else 'blue' if gnode.name in ('start', 'end'): colour = 'green' pos = gnode.pos if pos < 1e-6: pos = 0 dotfile.write( '\t"%s"\t [style=filled, color=%s, xlabel="@%s"];\n' % (gnode.fmt_name, colour, fmt_dec(pos))) for gnode in self.values(): for edge in gnode.fedges: colour = 'black' if edge.stretch else 'red' dotfile.write( '\t"%s" ->\t"%s" [ color=%s, label="%s%s" ];\n' % (gnode.fmt_name, edge.to_gnode.fmt_name, colour, fmt_dec(edge.size), '*' if edge.stretch else '')) dotfile.write('}\n') dotfile.close()
def draw(self, filename=None, opts={}, **kwargs): """ filename specifies the name of the file to produce. If None, the schematic is displayed on the screen. Note, if using Jupyter, then need to first issue command %matplotlib inline kwargs include: label_ids: True to show component ids label_values: True to display component values draw_nodes: True to show all nodes, False to show no nodes, 'primary' to show primary nodes, 'connections' to show nodes that connect more than two components, 'all' to show all nodes label_nodes: True to label all nodes, False to label no nodes, 'primary' to label primary nodes, 'alpha' to label nodes starting with a letter, 'pins' to label nodes that are pins on a chip, 'all' to label all nodes style: 'american', 'british', or 'european' scale: schematic scale factor, default 1.0 node_spacing: spacing between component nodes, default 2.0 cpt_size: size of a component, default 1.5 oversample: oversampling factor for png or pdf files help_lines: distance between lines in grid, default 0.0 (disabled) debug: True to display debug information """ for key, val in opts.items(): if key not in kwargs or kwargs[key] is None: kwargs[key] = val def in_ipynb(): try: ip = get_ipython() cfg = ip.config kernapp = cfg['IPKernelApp'] # Check if processing ipynb file. if 'connection_file' in kernapp: return True elif kernapp and kernapp[ 'parent_appname'] == 'ipython-notebook': return True else: return False except (NameError, KeyError): return False if not self.hints: raise RuntimeWarning('No schematic drawing hints provided!') png = 'png' in kwargs and kwargs.pop('png') svg = 'svg' in kwargs and kwargs.pop('svg') if not png and not svg: png = True if in_ipynb() and filename is None: if png: from IPython.display import Image, display_png pngfilename = tmpfilename('.png') self.tikz_draw(pngfilename, **kwargs) # Create and display PNG image object. # There are two problems: # 1. The image metadata (width, height) is ignored # when the ipynb file is loaded. # 2. The image metadata (width, height) is not stored # when the ipynb file is written non-interactively. display_png( Image(filename=pngfilename, width=self.width * 100, height=self.height * 100)) return if svg: from IPython.display import SVG, display_svg svgfilename = tmpfilename('.svg') self.tikz_draw(svgfilename, **kwargs) # Create and display SVG image object. # Note, there is a problem displaying multiple SVG # files since the later ones inherit the namespace of # the first ones. display_svg( SVG(filename=pngfilename, width=self.width * 100, height=self.height * 100)) return if filename is None: filename = tmpfilename('.png') self.tikz_draw(filename=filename, **kwargs) display_matplotlib(filename) return self.tikz_draw(filename=filename, **kwargs)