def visualize_bundles(trk, ren=None, inline=True, interact=False): """ Visualize bundles in 3D using fvtk """ if isinstance(trk, str): trk = nib.streamlines.load(trk) if ren is None: ren = fvtk.ren() for b in np.unique(trk.tractogram.data_per_streamline['bundle']): idx = np.where(trk.tractogram.data_per_streamline['bundle'] == b)[0] this_sl = list(trk.streamlines[idx]) sl_actor = fvtk.line(this_sl, Tableau_20.colors[int(b)]) fvtk.add(ren, sl_actor) if inline: tdir = tempfile.gettempdir() fname = op.join(tdir, "fig.png") fvtk.record(ren, out_path=fname) display.display_png(display.Image(fname)) if interact: fvtk.show(ren) return ren
def snap(title=None): if title: w = get_widget(title) else: w = get_window() display.display_png(capture_widget(w), raw=True)
def network(cls, entity, output_format='highres_image', **optional_parameters): """ Display STRING network image. Parameters ---------- output_format : str, optional 'image' or 'highres_image'. """ if isinstance(entity, str): identifiers = [entity] else: raise TypeError(f'The given identifier [{entity}] is invalid.') parameters = {'identifiers': '\r'.join(identifiers)} parameters.update(optional_parameters) url = "https://string-db.org/api/{}/network?{}".format( output_format, urllib.parse.urlencode(parameters)) response = requests.get(url) time.sleep(cls.wait) from IPython.display import Image, display_png display_png(Image(response.content))
def show_dot(s): from IPython.display import display_png, Image result = subprocess.run(['dot', '-Tpng'], input=s.encode('utf8'), stdout=subprocess.PIPE, check=True) img = Image(data=result.stdout, format='png') display_png(img)
def visualize_roi(roi, affine_or_mapping=None, static_img=None, roi_affine=None, static_affine=None, reg_template=None, scene=None, color=np.array([1, 0, 0]), opacity=1.0, inline=False, interact=False): """ Render a region of interest into a VTK viz as a volume """ if not isinstance(roi, np.ndarray): if isinstance(roi, str): roi = nib.load(roi).get_fdata() else: roi = roi.get_fdata() if affine_or_mapping is not None: if isinstance(affine_or_mapping, np.ndarray): # This is an affine: if (static_img is None or roi_affine is None or static_affine is None): raise ValueError( "If using an affine to transform an ROI, " "need to also specify all of the following", "inputs: `static_img`, `roi_affine`, ", "`static_affine`") roi = reg.resample(roi, static_img, roi_affine, static_affine) else: # Assume it is a mapping: if (isinstance(affine_or_mapping, str) or isinstance(affine_or_mapping, nib.Nifti1Image)): if reg_template is None or static_img is None: raise ValueError( "If using a mapping to transform an ROI, need to ", "also specify all of the following inputs: ", "`reg_template`, `static_img`") affine_or_mapping = reg.read_mapping(affine_or_mapping, static_img, reg_template) roi = auv.patch_up_roi( affine_or_mapping.transform_inverse( roi, interpolation='nearest')).astype(bool) if scene is None: scene = window.Scene() roi_actor = actor.contour_from_roi(roi, color=color, opacity=opacity) scene.add(roi_actor) if inline: tdir = tempfile.gettempdir() fname = op.join(tdir, "fig.png") window.snapshot(scene, fname=fname) display.display_png(display.Image(fname)) return _inline_interact(scene, inline, interact)
def png_h(self, b): fName = self.getFileName(self.path) if fName is None: return self.script_w.value = """from IPython.display import Image, display_png display_png(Image("%s")) # confusion_matrix """ % (fName) from IPython.display import Image, display_png display_png(Image("%s" % (fName)))
def _vispy_set_visible(self, visible): #self._backend2._vispy_set_visible(visible) if not visible: logger.warn('IPython notebook canvas cannot be hidden.') else: self._vispy_update() self._vispy_canvas.app.process_events() self._vispy_close() display_png(self._im, raw=True)
def show_image(filename): ''' Embed a PNG image with a filename relative to the current working directory into an IPython/Jupyter notebook. :param filename: :return: nothing ''' with open(filename, "rb") as f: image = f.read() display_png(image, raw=True)
def _inline_interact(scene, inline, interact): """ Helper function to reuse across viz functions """ if interact: window.show(scene) if inline: tdir = tempfile.gettempdir() fname = op.join(tdir, "fig.png") window.snapshot(scene, fname=fname, size=(1200, 1200)) display.display_png(display.Image(fname)) return scene
def _inline_interact(ren, inline, interact): """ Helper function to reuse across viz functions """ if interact: window.show(ren) if inline: tdir = tempfile.gettempdir() fname = op.join(tdir, "fig.png") window.record(ren, out_path=fname, size=(1200, 1200)) display.display_png(display.Image(fname)) return ren
def pyknp_dependency_visualize(result_list, withstr=False): for result in result_list: graph = [] bnst_dic = dict((x.bnst_id, x) for x in result.bnst_list()) for bnst in result.bnst_list(): if bnst.parent_id != -1: src_str = "".join(mrph.midasi for mrph in bnst.mrph_list()) dst_str = "".join(mrph.midasi for mrph in bnst_dic[bnst.parent_id].mrph_list()) graph.append((src_str, dst_str)) if withstr: print("{} => {}".format(src_str,dst_str)) g=pydot.graph_from_edges(graph,directed=True) #g.write_png("result.png") display_png(Image(g.create_png()))
def pyknp_dependency_visualize(comment_list, withstr=False): for sentence_list in comment_list: graph = [] for cid, chunk in enumerate(sentence_list): if chunk.dst != -1: src_str = str(cid) + ":" + chunk.midasi dst_str = str( chunk.dst) + ":" + sentence_list[chunk.dst].midasi graph.append((src_str, dst_str)) if withstr: print("{} => {}".format(src_str, dst_str)) g = pydot.graph_from_edges(graph, directed=True) #g.write_png("result.png") display_png(Image(g.create_png()))
def display_image(image): import io from matplotlib.image import imsave from IPython import display buf = io.BytesIO() imsave(buf, image) return display.display_png(display.Image(buf.getvalue()))
def run_dot(code, options=[], format='png'): # mostly copied from sphinx.ext.graphviz.render_dot dot_args = ['dot'] + options + ['-T', format] if os.name == 'nt': # Avoid opening shell window. # * https://github.com/tkf/ipython-hierarchymagic/issues/1 # * http://stackoverflow.com/a/2935727/727827 p = Popen(dot_args, stdout=PIPE, stdin=PIPE, stderr=PIPE, creationflags=0x08000000) else: p = Popen(dot_args, stdout=PIPE, stdin=PIPE, stderr=PIPE) wentwrong = False try: # Graphviz may close standard input when an error occurs, # resulting in a broken pipe on communicate() stdout, stderr = p.communicate(code.encode('utf-8')) except (OSError, IOError) as err: if err.errno != EPIPE: raise wentwrong = True except IOError as err: if err.errno != EINVAL: raise wentwrong = True if wentwrong: # in this case, read the standard output and standard error streams # directly, to get the error message(s) stdout, stderr = p.stdout.read(), p.stderr.read() p.wait() if p.returncode != 0: raise RuntimeError('dot exited with error:\n[stderr]\n{0}' .format(stderr.decode('utf-8'))) return display_png(stdout,raw=True)
def cabocha_dependency_visualize(novel_list, withstr=False): for sentence_list in novel_list: graph = [] for chunk in sentence_list: src_str=chunk.surfaces() dst_str="" if chunk.dst!=-1: dst_str=sentence_list[chunk.dst].surfaces() if src_str!="" and dst_str!="": graph.append((src_str,dst_str)) if withstr: print("{} => {}".format(src_str,dst_str)) g=pydot.graph_from_edges(graph,directed=True) #g.write_png("result.png") display_png(Image(g.create_png()))
def plot_md_info(directory=None, run_list=None, plots_to_skip=DEFAULT_PLOTS_TO_SKIP): '''Find run folders of given enzyme and show plots. Use in jupyter notebook. Args: directory (str): path to enzymeA ie. ~/MD/enzymeA/ The folder ~/MD/enzymeA must contain folers in format runX/analyze where x is number 1+. and analyze contains results from cpptraj analysis and plots plotted in gnuplot by Sergio's script. run_list (list): a list of runs which to analyze. defaults to all runs found in directory kwarg plots_to_skip (list): a list of filenames (.png) you do not want to plot ''' if not directory or not os.path.isdir(directory): raise FileNotFoundError( "Please specify existing folder containing MD runs!") if not run_list: run_list = sorted(glob.glob(os.path.join(directory, "run?"))) if not plots_to_skip: plots_to_skip = [] print('''Plotting MD info from folder {}\n about runs {}\n excluding plots: {}'''.format( directory, list(map(lambda x: x.split("/")[-1], run_list)), plots_to_skip)) os.chdir(directory) print() for run in run_list: images = glob.glob(run.split('.')[0] + "/analyze/*png") images = [ img for img in images if img.split('/')[-1] not in plots_to_skip ] print("#" * 80) print("-" * 35, run.split('/')[-1], "-" * 35) print("#" * 80) for image in images: display_png(Image(filename=image))
def _build_fig(self, figtype, figname, figsize=None, title=None, tight=True): if self.out_path: print("(DrawEngine) drawing %s..." % figname) #### sns ##### sns.set_style("whitegrid") # marker bug sns.set_context(rc={'lines.markeredgewidth': 0.5}) fig = plt.figure(figsize=figsize) fig.clear() if title is None: title = figname else: title = "%s: %s" % (figname, title) fig.suptitle("%s %s" % (title, figtype), fontsize=14) yield fig if tight: fig.tight_layout(rect=[0, 0.03, 1, 0.95]) if self.out_path: fig.savefig(self.out_path + figname + "_" + figtype + ".png") else: from IPython.display import display_png from IPython.core.pylabtools import print_figure data = print_figure(fig, "png") display_png(data, raw=True) plt.close(fig) if self.out_path: print("ok")
def plot_md_info(directory=None, run_list=None, plots_to_skip=DEFAULT_PLOTS_TO_SKIP): '''Find run folders of given enzyme and show plots. Use in jupyter notebook. Args: directory (str): path to enzymeA ie. ~/MD/enzymeA/ The folder ~/MD/enzymeA must contain folers in format runX/analyze where x is number 1+. and analyze contains results from cpptraj analysis and plots plotted in gnuplot by Sergio's script. run_list (list): a list of runs which to analyze. defaults to all runs found in directory kwarg plots_to_skip (list): a list of filenames (.png) you do not want to plot ''' if not directory or not os.path.isdir(directory): raise FileNotFoundError("Please specify existing folder containing MD runs!") if not run_list: run_list = sorted(glob.glob(os.path.join(directory,"run?"))) if not plots_to_skip: plots_to_skip=[] print('''Plotting MD info from folder {}\n about runs {}\n excluding plots: {}'''.format(directory, list(map(lambda x: x.split("/")[-1], run_list)), plots_to_skip)) os.chdir(directory) print() for run in run_list: images = glob.glob(run.split('.')[0]+"/analyze/*png") images = [img for img in images if img.split('/')[-1] not in plots_to_skip] print("#"*80) print("-"*35,run.split('/')[-1],"-"*35) print("#"*80) for image in images: display_png(Image(filename=image))
def draw(self, filename=None, **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 or 'none' 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 or 'none' 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, 'none' to label no 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 dpi: dots per inch for png files help_lines: distance between lines in grid, default 0.0 (disabled) debug: True to display debug information """ # None means don't care, so remove. for key in list(kwargs.keys()): if kwargs[key] is None: kwargs.pop(key) # Remove options that may be overridden for elt in self.elements.values(): for key in list(elt.opts.keys()): if key in kwargs: elt.opts.remove(key) # Default options opts = SchematicOpts() for key, val in opts.items(): if key not in kwargs: kwargs[key] = val # Global options (at end of list for historical reasons) for eltname in reversed(self.elements): elt = self.elements[eltname] if not elt.directive: break for key, val in elt.opts.items(): # val is a str kwargs[key] = val def in_ipynb(): try: ip = get_ipython() cfg = ip.config kernapp = cfg['IPKernelApp'] # Check if processing ipynb file for Jupyter notebook. 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!') if in_ipynb() and filename is None: png = 'png' in kwargs and kwargs.pop('png') svg = 'svg' in kwargs and kwargs.pop('svg') if not png and not svg: svg = False if svg: try: 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=svgfilename)) return except: pass 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. width, height = png_image_size(pngfilename) # width, height specify the image dimension in pixels display_png(Image(filename=pngfilename, width=width, height=height)) return if filename is None: filename = tmpfilename('.png') # Thicken up lines to reduce aliasing causing them to # disappear, especially when using pdftoppm. self.tikz_draw(filename=filename, options='bipoles/thickness=2', **kwargs) display_matplotlib(filename, self.dpi) return self.tikz_draw(filename=filename, **kwargs)
#学習用データと検証用データに分ける train, test = chainer.datasets.split_dataset_random(data, int(N * 0.8)) train_iter = chainer.iterators.SerialIterator(train, n_batchsize, shuffle=False) test_iter = chainer.iterators.SerialIterator(test, n_batchsize, repeat=False, shuffle=False) updater = LSTMUpdater(train_iter, optimizer, device=-1) trainer = training.Trainer(updater, (n_epoch, "epoch"), out="result") trainer.extend(extensions.Evaluator(test_iter, model, device=-1)) trainer.extend(extensions.LogReport(trigger=(10, "epoch"))) # 1エポックごとにログ出力 trainer.extend(extensions.PrintReport( ["epoch", "main/loss", "validation/main/loss", "main/accuracy", "validation/main/accuracy", "elapsed_time"])) # エポック、学習損失、テスト損失、学習正解率、テスト正解率、経過時間 trainer.extend(extensions.PlotReport(['main/loss', 'val/main/loss'], x_key='epoch', file_name='loss.png')) trainer.extend(extensions.PlotReport(['main/accuracy', 'val/main/accuracy'], x_key='epoch', file_name='accuracy.png')) trainer.run() # In[42]: from IPython.display import Image, display_png display_png(Image("./result/accuracy.png")) # In[43]: display_png(Image("./result/loss.png")) # In[ ]:
plt.plot(lr.coef_, 'o', label="LinearRegression") plt.xlabel("Coefficient index") plt.ylabel("Coefficient magnitude") plt.hlines(0, 0, len(lr.coef_)) plt.ylim(-25, 25) plt.legend() # training set size を変えてスコアがどう変化するか # sklearn.model_selection.learning_curve を用いてplotしている # https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.learning_curve.html mglearn.plots.plot_ridge_n_samples() from IPython.display import Image, display_png display_png(Image("figures/chapter02/20160706172820.png")) # https://jojoshin.hatenablog.com/entry/2016/07/06/180923 from sklearn.linear_model import Lasso lasso = Lasso().fit(X_train, y_train) print("Training set score: {:.2f}".format(lasso.score(X_train, y_train))) print("Test set score: {:.2f}".format(lasso.score(X_test, y_test))) print("Number of features used: {}".format(np.sum(lasso.coef_ get_ipython().getoutput("= 0)))") lasso001 = Lasso(alpha=0.01, max_iter=100000).fit(X_train, y_train) print("Training set score: {:.2f}".format(lasso001.score(X_train, y_train))) print("Test set score: {:.2f}".format(lasso001.score(X_test, y_test))) print("Number of features used: {}".format(np.sum(lasso001.coef_ get_ipython().getoutput("= 0)))")
"DL with Spark Deep Cognition").getOrCreate() sc = spark.sparkContext import glob fs = glob.glob("flower_photos/sample/*.jpg") import IPython.display as dp # create list of image objects images = [] for ea in fs: images.append(dp.Image(filename=ea, format='png')) # display all images for ea in images: dp.display_png(ea) from pyspark.ml.image import ImageSchema image_df = ImageSchema.readImages("flower_photos/sample/") image_df.show() from pyspark.ml.image import ImageSchema from pyspark.sql.functions import lit from sparkdl.image import imageIO from keras.applications import InceptionV3 model = InceptionV3(weights="imagenet") model.save('model-full.h5') # saves to the local filesystem from keras.applications.inception_v3 import preprocess_input
### TIPS 3/3 log files To monitor training, `exp/train*/results/` contains useful files: - `loss.png` train/valid loss values - `acc.png` train/valid accuracy - `cer.png` train/valid character error rate - `att_ws/*.png` attention plots """ import glob from IPython.display import Image, display_png expdir = "espnet/egs/an4/asr1/exp/train_nodev_pytorch_train_mtlalpha0.5/results/" for name in ["loss.png", "acc.png", "cer.png"]: print(name) display_png(Image(expdir + name, width=500)) """### Attention plots (RNN) A diagonal attention is good measure to check training in ASR <table> <tr> <td><img src="https://github.com/espnet/interspeech2019-tutorial/blob/master/notebooks/interspeech2019_asr/figs/4k0c0302.ep.1.png?raw=1"></td> <td><img src="https://github.com/espnet/interspeech2019-tutorial/blob/master/notebooks/interspeech2019_asr/figs/4k0c0302.ep.15.png?raw=1"></td> </tr> </table> RNN attentions at the first (left) and last (right) epoch ### Attention plots (Transformer)
self.srcs = [] # 係り元文節インデックス番号のリスト self.dst = dst # 係り先文節インデックス番号 # 係り元を代入,Chunkリストを文のリストを追加 def append_sentence(chunks, sentences): # 係り元を代入 for i, chunk in enumerate(chunks): if chunk.dst != -1: chunks[chunk.dst].srcs.append(i) sentences.append(chunks) return sentences import np41sss sentences = np41sss.Ai_morphs() sentence = sentences[1] edges = [] for id, chunk in enumerate(sentence): if int(chunk.dst) != -1: modifier = ''.join([morph.surface if morph.pos != '記号' else '' for morph in chunk.morphs] + ['(' + str(id) + ')']) modifiee = ''.join([morph.surface if morph.pos != '記号' else '' for morph in sentence[int(chunk.dst)].morphs] + ['(' + str(chunk.dst) + ')']) edges.append([modifier, modifiee]) n = pydot.Node('node') n.fontname = 'IPAGothic' graph = pydot.graph_from_edges(edges, directed=True) graph.add_node(n) graph.write_png('./kakarigi44.png') display_png(Image('./kakarigi44.png'))
def show_ipython(self): """Show the image in IPython as a PNG image.""" png = image_to_png(self.img) display_png(png, raw=True)
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, 'none' to label no 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 """ # Add schematic options defined with ;; are stored in the append # field of opts. 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!') if in_ipynb() and filename is None: png = 'png' in kwargs and kwargs.pop('png') svg = 'svg' in kwargs and kwargs.pop('svg') if not png and not svg: svg = False if svg: try: 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=svgfilename)) return except: pass 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 filename is None: filename = tmpfilename('.png') # Thicken up lines to reduce aliasing causing them to # disappear, especially when using pdftoppm. self.tikz_draw(filename=filename, preamble='bipoles/thickness=2', **kwargs) display_matplotlib(filename) return self.tikz_draw(filename=filename, **kwargs)
def draw(self, filename=None, args=None, stretch=1, scale=1, **kwargs): self.node_spacing = 2 * stretch * scale self.cpt_size = 1.5 * scale self.scale = scale 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 = self._tmpfilename('.png') self.tikz_draw(pngfilename, args=args, **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 = self._tmpfilename('.svg') self.tikz_draw(svgfilename, args=args, **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 display = False if filename is None: filename = self._tmpfilename('.png') display = True self.tikz_draw(filename=filename, args=args, **kwargs) if display: # TODO display as SVG so have scaled fonts... from matplotlib.pyplot import figure from matplotlib.image import imread img = imread(filename) fig = figure() ax = fig.add_subplot(111) ax.imshow(img) ax.axis('equal') ax.axis('off')
可視化には,Graphviz等を用いるとよい. ''' from knock41 import load_chunk import pydot from IPython.display import Image, display_png from graphviz import Digraph if __name__ == '__main__': filepath = './data/ai.ja/ai.ja.txt.parsed' res = load_chunk(filepath) res = res[7] edges = [] for id, chunk in enumerate(res.chunks): if int(chunk.dst) != -1: modifier = ''.join([ morph.surface if morph.pos != '記号' else '' for morph in chunk.morphs ] + ['(' + str(id) + ')']) modifiee = ''.join([ morph.surface if morph.pos != '記号' else '' for morph in res.chunks[int(chunk.dst)].morphs ] + ['(' + str(id) + ')']) edges.append([modifier, modifiee]) n = pydot.Node('node') n.fontname = 'IPAGothic' g = pydot.graph_from_edges(edges, directed=True) g.add_node(n) g.write('./ans44.png') display_png(Image('./ans44.png'))
#init_img = vgg16.preprocess_input(init_img) #print(init_img.shape) init_img = preprocess_forvgg(base_img, output_img_width, output_img_height) # change noise image #print(input_base_img.shape) evaluator = Evaluator() loss_list = [] for i in range(iter): print('Start of iteration', i) start_time = time.time() init_img, min_val, info = fmin_l_bfgs_b(evaluator.loss, init_img.flatten(), args = (output_img_width, output_img_height), fprime=evaluator.grads, maxfun=20) loss_list.append(min_val) print('Current loss value:', min_val) # save current generated image img = deprocess_image(init_img.copy(), output_img_width, output_img_height) fname = output_image_path + '_at_iteration_%02d.png' % i if(not FLAG_JUPYTER): image.save_img(fname, img) else: pilImg = Image.fromarray(np.uint8(img)) pilImg.save(fname, 'PNG', quality=100, optimize=True) IPd.display_png(IPd.Image(fname)) end_time = time.time() print('Image saved as', fname) print('Iteration %d completed in %ds' % (i, end_time - start_time)) if(FLAG_JUPYTER): %matplotlib inline plt.plot(range(0, iter), loss_list)
def draw_image(): clear_output(wait=True) image_file = img_dir + '/' + random.choice(files) display_png(Image(image_file))
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)
features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] bumpy_fast = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] grade_slow = [ features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] bumpy_slow = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] # You will need to complete this function imported from the ClassifyNB script. # Be sure to change to that code tab to complete this quiz. clf = classify(features_train, labels_train) ### draw the decision boundary with the text points overlaid prettyPicture(clf, features_test, labels_test, "./test.png") output_image("test.png", "png", open("test.png", "rb").read()) from IPython.display import display_png display_png("./test.png") print "done" # In[6]:
def show_image(filename): with open(filename, "rb") as f: image = f.read() display_png(image, raw=True)
def rman(self, line, cell) : "executes the cell contents as RenderMan, and displays returned graphical output.\n" \ "Usage:\n" \ "\n" \ " %%rman «options...»\n" \ "\n" \ "where valid options are\n" \ " --debug keeps temp files for debugging\n" \ " --source-search=«dir» optional directories to search for files referenced in submagics\n" \ " --timeout=«timeout» specifies how many seconds to wait for" \ " subprocesses to respond (default is infinite)\n" \ " --verbose=«verbosity» verbosity level 0-3, default is 1\n" \ "\n" \ " --archive-search=«dir» optional directory for Aqsis to find “archive” .rib files\n" \ " --post-process=«code» run the specified Python «code» after generating all the images." \ " Must be used in conjunction with --return-var, and you should include" \ " the specified variable name somewhere in «code».\n" \ " --procedural-search=«dir» optional directories for Aqsis to find procedural shader libraries\n" \ " --resource-search=«dir» optional directories to search for files referenced" \ " in submagics and for Aqsis to find additional files\n" \ " --return-var=«varname» return images as a sequence of PNG byte objects in this" \ " variable instead of displaying them\n" \ " --shader-search=«dir» optional directories for Aqsis to find additional compiled shader files\n" \ " --texture-search=«dir» optional directories for Aqsis to find texture files" timeout = None debug = False source_search = None map_aqsis_opts = \ { "archive-search" : ("archives", True, True), # I don’t think I need “displays” "shader-search" : ("shaders", True, True), "procedural-search" : ("procedurals", True, True), # “resources” handled specially "texture-search" : ("textures", True, True), "progress" : ("Progress", False, False), "verbose" : ("verbose", True, False), } aqsis_opts = {} def save_search_path(search_type, new_value) : # sets a new value for a search-path option, including the old # value where there is a “&” item. nonlocal source_search if search_type == "sources" : old_value = source_search else : old_value = aqsis_opts.get(search_type) #end if if old_value == None : old_value = "&" #end if collect = [] for item in new_value.split(":") : if item == "&" : collect.extend(old_value.split(":")) else : collect.append(item) #end if #end for new_value = ":".join(collect) if search_type == "sources" : source_search = new_value else : aqsis_opts[search_type] = new_value #end if #end save_search_path #begin rman opts, args = getopt.getopt \ ( shlex.split(line), "", ("debug", "post-process=", "resource-search=", "return-var=", "source-search=", "timeout=",) + tuple(k + ("", "=")[map_aqsis_opts[k][1]] for k in map_aqsis_opts) ) if len(args) != 0 : raise getopt.GetoptError("unexpected args") #end if return_var = None post_process = None for keyword, value in opts : if keyword == "--debug" : debug = True elif keyword == "--resource-search" : for k in ("sources", "resources") : save_search_path(k, value) #end for elif keyword == "--post-process" : post_process = value elif keyword == "--return-var" : return_var = value elif keyword == "--source-search" : save_search_path("sources", value) elif keyword == "--timeout" : timeout = float(value) elif keyword.startswith("--") and keyword[2:] in map_aqsis_opts : mapname, has_value, is_search_path = map_aqsis_opts[keyword[2:]] if is_search_path : assert has_value save_search_path(mapname, value) else : aqsis_opts[mapname] = (None, value)[has_value] #end if #end if #end for if post_process != None and return_var == None : raise getopt.GetoptError("--post-process requires --return-var") #end if images = self.run_aqsis \ ( input = cell, timeout = timeout, debug = debug, source_search = source_search, aqsis_opts = aqsis_opts ) result = None if return_var != None : get_ipython().push({return_var : images}) if post_process != None : get_ipython().ex(post_process) #end if else : if len(images) != 0 : for image in images : display_png(image, raw = True) #end for else : result = "No output!" #end if #end if return \ result