def subplots(*args, **kwargs): fig, axs = matplotlib.pyplot.subplots(*args, **kwargs) yield fig, axs try: display(fig) finally: matplotlib.pyplot.close(fig)
def figure(*args, **kwargs): fig = matplotlib.pyplot.figure(*args, **kwargs) yield(fig) try: display(fig) finally: matplotlib.pyplot.close(fig)
def plot(self, positions, dt=0.1): ''' This function animates the motion of the particles. It is only meant as a demonstration for small N, to check that things are working as we expect. It takes in a list of calculated positions, and displays where they were at a specified regular time interval. ''' skip = dt / self.timestep for i in np.arange(0, len(positions), skip): i = int(i) fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(111, projection='3d') pos = positions[i] for j in range(len(pos)): ax.scatter(pos[j][0], pos[j][1], pos[j][2], color="blue") ax.set_xlim([-1, self.size[0] + 1]) ax.set_ylim([-1, self.size[1] + 1]) ax.set_zlim([-1, self.size[2] + 1]) plt.grid() clear_output(wait=True) display(fig) fig.clear() plt.close()
def print_topics(cls, model, num_words: int = 10) -> None: """Print all of the latent topics and top tokens associated with each topic. Args: model: The gensim model for which to calculate the coherence. num_words: Optional; The amount of words related to each topic to print. The default behaviors is to print the top 10 words related to each latent topic. """ topics = TopicModeler.get_topics(model, num_words=num_words) topics_dict = [] for t in topics: topics = t[1].replace('"', "").replace('+', "\n").split('\n') topics = [topic.strip().split('*') for topic in topics] keyphrases = ", ".join([key for _, key in topics]) topics_dict.append([t[0] + 1, keyphrases]) df = pd.DataFrame(topics_dict, columns=["Topic #", "Top keywords"]) df = df.style.set_properties(**{ "text-align": "left", "colheader-align": "left" }) display(df)
def bodePlot(H_s, w_range=(0, 8), points=800): """Plot the magnitude and phase of H_s over the given range of frequencies.""" w = logspace(*w_range, points) h_s = lambdify(s, H_s, 'numpy') H_jw = h_s(1j * w) # find mag and phase mag = 20 * np.log10(np.abs(H_jw)) phase = angle(H_jw, deg=True) eqn = Eq(H, simplify(H_s)) display(eqn) fig, axes = plt.subplots(1, 2, figsize=(18, 6)) ax1, ax2 = axes[0], axes[1] # mag plot ax1.set_xscale('log') ax1.set_ylabel('Magntiude in dB') ax1.set_xlabel('$\omega$ in rad/s') ax1.plot(w, mag) ax1.grid() ax1.set_title("Magnitude of $H(j \omega)$") # phase plot ax2.set_ylabel('Phase in degrees') ax2.set_xlabel('$\omega$ in rad/s') ax2.set_xscale('log') ax2.plot(w, phase) ax2.grid() ax2.set_title("Phase of $H(j \omega)$") plt.show()
def draw_strokes(data, factor=0.2, svg_filename = '/tmp/sketch_rnn/svg/sample.svg'): os.makedirs(os.path.dirname(svg_filename), exist_ok=True) min_x, max_x, min_y, max_y = get_bounds(data, factor) dims = (50 + max_x - min_x, 50 + max_y - min_y) dwg = svgwrite.Drawing(svg_filename, size=dims) dwg.add(dwg.rect(insert=(0, 0), size=dims,fill='white')) lift_pen = 1 abs_x = 25 - min_x abs_y = 25 - min_y p = "M%s,%s " % (abs_x, abs_y) command = "m" for i in range(len(data)): if (lift_pen == 1): command = "m" elif (command != "l"): command = "l" else: command = "" x = float(data[i,0])/factor y = float(data[i,1])/factor lift_pen = data[i, 2] p += command+str(x)+","+str(y)+" " the_color = "black" stroke_width = 1 dwg.add(dwg.path(p).stroke(the_color,stroke_width).fill("none")) dwg.save() display(SVG(dwg.tostring()))
def load_graph(num): global G while num < 1 or num > 5: num = input("Invalid Graph number, please enter a new one: ") num = int(num) fileName = "" if num == 1: # set the file name fileName = "simpleG.jpg" G.create_simple() elif num == 2: # set the file name fileName = "simp_diff_len.jpg" G.create_simple_different_road_lengths() elif num == 3: # set the file name fileName = "simp_diff_ang.jpg" G.create_simple_different_angles() elif num == 4: # set the file name fileName = "mystery.jpg" G.mystery_graph() elif num == 5: # set the file name fileName = "complex.jpg" G.create_complex() # display the image here display( Image(filename=('/home/pi/Dashboard/user/jason/MyImages/' + fileName), width=300, height=200))
def load(self, *_): try: self.set_buzy(True, "⌛ Loading data...") if self._output: with pd.option_context('display.precision', 2, 'display.max_columns', 300): pd.options.display.max_columns = 300 self._output.clear_output() style_dict = [{ 'selector': 'thead th', 'props': [('position', 'sticky'), ('top', '0'), ('background-color', 'grey')], }] with self._output: # pylint: disable=not-context-manager df: pd.DataFrame = (self.data.style.format({ 'H': "{:.2%}" }).set_table_styles(style_dict).hide_index()) display(HTML(df.style.render())) if self._table is not None: self._table.clear() self._table.load(self.data) self.set_buzy(False, "✔") except Exception as ex: self.set_buzy(False, f"😮 {str(ex)}")
def display_vctk_sample(vctk_speaker_id, sample_id): speaker_info = get_vctk_speaker_info() # raw .wav file wav_file = os.path.join( vctk_raw_folder, 'wav48/p' + str(vctk_speaker_id) + '/p' + str(vctk_speaker_id) + '_' + '{num:03d}'.format(num=sample_id) + '.wav') # print speaker info display(speaker_info[speaker_info['id'] == vctk_speaker_id]) # print text txt_file = wav_file.replace("wav48", "txt").replace(".wav", ".txt") f = open(txt_file, 'r') print(f.read()) f.close() # play sample rate, wav_data = wavfile.read(wav_file) display(IPython.display.Audio(data=wav_data, rate=rate, autoplay=True)) # plot waveform plt.figure(figsize=(15, 4)) plt.plot(wav_data) plt.grid(True) plt.xlim(0, len(wav_data)) plt.xticks(range(0, len(wav_data), 48000), range(0, np.int(np.ceil(1.0 * len(wav_data) / 48000)))) # plot spectrogram plot_spectrogram(wav_data, rate)
def tune_hyper_parameter(stock_name, df, config): # Get last 365 day for tuning df = df[-365:] bayer_max_evals = config.get('bayer_max_evals', 1000) param_grid = config.get('param_grid') # Hyperparameter grid bayes_trials = Trials() # Create the algorithm bayes_algo = tpe.suggest fmin_objective = partial(objective, df=df) bayes_best = fmin(fn=fmin_objective, space=param_grid, algo=bayes_algo, trials=bayes_trials, max_evals=bayer_max_evals) param_file_path = os.path.join(config['model_dir'], '%s_param.txt' % (stock_name)) with open(param_file_path, 'w') as outfile: json.dump(bayes_best, outfile) display(bayes_best)
def display_img_df( df, txt_fields=None, img_fields=["thumbnail"], link_fields=[], use_api=True, img_first=True, docid_name="dsid", script_func_name=None, script_name=None, **kwargs ): """ image display를 위해서 html로 변환하여 화면에 표시 img_fields에 정의된 필드는 excaping하지 않는다. use_api : dsid(docimgdsid) 필드가 있으면 가 있으면 api로 필요한 데이터를 가져온다. """ if len(df) == 0: return df = _complete_img_df(df, txt_fields, img_fields, link_fields, use_api, img_first, docid_name=docid_name, **kwargs) html = df.to_html(escape=False) df_name = kwargs.get("script_df_name", "None") if script_func_name is not None: if script_name is None: script_name = script_func_name html = add_script(script_func_name, script_func_name, html, df_name=df_name) display(HTML(html))
def fromTHnArray(cls, tHnArray, inputProjectionList, figureOptions, graphOptions): """ initialize bokehDrawHisto from histogramArray and inputProjectionArray :param tHnArray: TContainer with histograms :param inputProjectionList: :param graphOptions: :param figureOptions: :return: """ self: bokehDrawHisto = cls() self.figureOptions.update(figureOptions) self.graphOptions.update(graphOptions) self.projectionList = inputProjectionList self.histogramList = {} for his in tHnArray: try: # check if root THn - otherwise assume ddHis = thnToNumpyDD(his) logging.info("Processing histogram %s", his.GetName()) except: #logging.error("non compatible object %s\t%s", his.GetName(), his.__class__) ddHis = his logging.info("Processing histogram %s", ddHis['name']) self.histogramList[ddHis['name']] = ddHis self.__processSliders() self.sliderWidgets = widgets.VBox(self.sliderList) self.__processProjections() isNotebook = get_ipython().__class__.__name__ == 'ZMQInteractiveShell' #self.handle = show(self.sliderWidgets, notebook_handle=isNotebook) display(self.sliderWidgets) return self
def protect (author=None): """Makes current notebook protected with the given author name. Protected notebooks won't be saved unless the user matches the author.""" author = author or os.environ['USER'] display(Javascript("document.radiopadre.protect('%s')"%author)) display(HTML(render_status_message("""This notebook is now protected, author is "%s". All other users will have to treat this notebook as read-only.""" % author)))
def show(close=None, block=None): """Show all figures as SVG/PNG payloads sent to the IPython clients. Parameters ---------- close : bool, optional If true, a ``plt.close('all')`` call is automatically issued after sending all the figures. If this is set, the figures will entirely removed from the internal list of figures. block : Not used. The `block` parameter is a Matplotlib experimental parameter. We accept it in the function signature for compatibility with other backends. """ if close is None: close = InlineBackend.instance().close_figures try: for figure_manager in Gcf.get_all_fig_managers(): display(figure_manager.canvas.figure) finally: show._to_draw = [] # only call close('all') if any to close # close triggers gc.collect, which can be slow if close and Gcf.get_all_fig_managers(): matplotlib.pyplot.close('all')
def rank_features_by_logreg_coefficients(X_train, logreg, abs_threshold=0.5): fi = { 'Features': X_train.columns.tolist(), 'Importance': np.transpose(logreg.coef_[0]) } importance = pd.DataFrame(fi, index=None).sort_values( 'Importance', ascending=False).set_index('Features').sort_values(by='Importance', ascending=False) mask = abs(importance['Importance']) > abs_threshold with pd.option_context('display.max_rows', len(importance)): display(importance[mask]) # Creating graph title titles = [ 'The most important features in predicting survival on the Titanic: Logistic Regression' ] # Plotting graph importance_plotting(importance.reset_index(), 'Importance', 'Features', 'Reds_r', titles) important_features = importance[mask].index.tolist() unimportant_features = importance[~mask].index.tolist() return important_features, unimportant_features
def tikz_to_plot( cls, code, x='1em', y='1em', scale='1', svg_scale=2, raw=False, **kwargs, ): code = cls.tikz_to_tex(code, x=x, y=y, scale=scale, raw=raw, **kwargs) try: with tempfile.TemporaryDirectory() as tmpdir: tmpdir = Path(tmpdir) main_tex = (tmpdir / 'main.tex') main_tex.write_text(code) run_process(['pdflatex', 'main.tex'], cwd=tmpdir) run_process([ 'inkscape', '--without-gui', '--file=main.pdf', '--export-plain-svg=main.svg', ], cwd=tmpdir) res_svg = Path(tmpdir / 'main.svg').read_text() return SVG(resize_svg_code(res_svg, factor=svg_scale, method='bs4')) except Exception as e: display(Code(code, language='tex')) raise
def show_style(): html = HTML('''<style> .right_aligned_df td { text-align: right; } .left_aligned_df td { text-align: right; } .pink_df { background-color: pink; } </style>''') display(html)
def nb_raw_input(prompt, name="raw_input_reply"): display( Javascript( """ var dialog = $('<div/>').append( $('<input/>') .attr('id', 'theinput') .attr('value', '') ); $(document).append(dialog); dialog.dialog({ resizable: false, modal: true, title: "%s", closeText: '', buttons : { "Okay": function () { IPython.notebook.kernel.execute( "%s = '" + $("input#theinput").attr('value') + "'" ); $(this).dialog('close'); dialog.remove(); }, "Cancel": function () { $(this).dialog('close'); dialog.remove(); } } }); """ % (prompt, name) ), include=["application/javascript"], )
def set_data(self, data_dict, download=False, load_transform=None, **kwargs): # get data from urls in data_dict if download: data = [] for i, (id, url) in enumerate(data_dict.items()): clear_output(wait=True) display('downloading data') display('progress: %g%%' % (100. * (i + 1) / len(data_dict))) d = self.download_image(url, id, download) if d is None: continue d = functions.kwarg_fn( [self, functions, list, dict, __builtins__, np, torch], d, **kwargs) if load_transform: d = load_transform(d) data.append(d) if np.all([ type(d) is torch.Tensor and d.ndimension() == 4 for d in data ]): self.data = torch.cat(data) else: self.data = data else: self.data = data_dict.copy()
def _show_thumbs(images, width=None, ncol=None, maxwidth=None, mincol=None, external_thumbs=None, maxcol=None, title=None, **kw): if not images: return None nrow, ncol, width = radiopadre.file.compute_thumb_geometry( len(images), ncol, mincol, maxcol, width, maxwidth) npix = int(radiopadre.DPI * width) # make list of basename, filename tuples filelist = sorted( [(os.path.basename(img.fullpath), img.fullpath) for img in images]) # keep track of thumbnail fails nfail = 0 html = render_preamble() + render_title(title) + \ """<br> <table style="border: 0px; text-align: left">\n """ for row in range(nrow): html += """<tr style="border: 0px; text-align: left">\n""" filelist_row = filelist[row * ncol:(row + 1) * ncol] for name, image in filelist_row: html += """<td style="border: 0px; text-align: center">""" html += "<a href=%s target='_blank'>%s</a>" % (render_url(image), name) html += "</td>\n" html += """</tr><tr style="border: 0px; text-align: left">\n""" for _, image in filelist_row: if external_thumbs is False: thumb = None # make thumbnail and record exceptions. Print the first one, as # they really shouldn't happen else: try: thumb = _make_thumbnail(image, npix) if not thumb and external_thumbs: nfail += 1 except: if not nfail: traceback.print_exc() nfail += 1 thumb = None html += """<td style="border: 0px; text-align: left">""" if thumb: html += "<a href=%s target='_blank'><img src=%s alt='?'></a>" % ( render_url(image), render_url(thumb)) else: html += "<a href=%s target='_blank'><img src=%s width=%d alt='?'></a>" % ( render_url(image), render_url(image), npix) html += "</td>\n" html += "</tr>\n" html += "</table>" if nfail: html += "(WARNING: %d thumbnails unexpectedly failed to generate, check console for errors)<br>\n" % nfail display(HTML(html))
def transform(): global file_name imageLocation = 'photo/zumi.png' if (file_name != ''): imageLocation = 'photo/' + file_name + ".png" #RotaionRatio = 0.5 # 회전시 잘리는 경우 1을 입력 RotaionRatio = 1 # 회전시 잘리는 경우 1을 입력 wImagR = widgets.Image( layout=Layout(border="solid"), width=300, height=400 # 이미지 크기 ) wRadioR = widgets.RadioButtons( options=['원위치', '왼쪽으로 90도 회전', '오른쪽으로 90도 회전', '좌우 반전', '상하 반전'], description='Rotate:', disabled=False) def on_Rotate_change(change): ImageRotate(change['new']) def ImageRotate(direction): image = cv2.imread(imageLocation) # 이미지 읽기 (height, width) = image.shape[:2] (center_height, center_width) = (height / 2.0, width / 2.0) if (direction == '왼쪽으로 90도 회전'): angle = 90 M = cv2.getRotationMatrix2D((center_width, center_height), angle, RotaionRatio) image = cv2.warpAffine(image, M, (width, height)) elif (direction == '오른쪽으로 90도 회전'): angle = -90 M = cv2.getRotationMatrix2D((center_width, center_height), angle, RotaionRatio) image = cv2.warpAffine(image, M, (width, height)) elif (direction == '좌우 반전'): image = cv2.flip(image, 1) elif (direction == '상하 반전'): image = cv2.flip(image, 0) originalImg = cv2.imencode(".png", image)[1].tostring() wImagR.value = originalImg ImageRotate('원위치') wRadioR.observe(on_Rotate_change, names='value') BOX_NEW = widgets.Box([wImagR, wRadioR]) display(BOX_NEW)
def _pngDisplay(self): ofile = tempfile.NamedTemporaryFile(suffix=".png") with _setIgnoreLevel(ROOT.kError): self.canvas.SaveAs(ofile.name) img = IPython.display.Image(filename=ofile.name, format='png', embed=True) display(img)
def csnap(orig_df, fn=lambda x: x.shape, msg=None): """ Custom Help function to print things in method chaining. Returns back the df to further use in chaining. """ if msg: print(msg) display(fn(orig_df)) return orig_df
def progresser(x,y,z): if len(apsc.history())<10: plt.xlim(1,10) else: plt.xlim(1,len(apsc.history())) plt.plot(scale(apsc.history()), 'g') clear_output(True) display(f)
def interact_h(_interact_f, *args, **kwargs): '''Similar to IPython's interact function, but with widgets packed horizontally.''' f = _interact_f w = widgets.interactive(f, *args, **kwargs) f.widget = w # Weirdly enough, be sure to display before changing the class. display(w) return f
def summary(self, **kw): kw.setdefault('title', self._title) kw.setdefault('showpath', self._showpath) summary = getattr(self._classobj, "_show_summary", None) if summary: display(HTML(render_refresh_button(full=self._parent and self._parent.is_updated()))) return summary(self, **kw) else: return self.list(**kw)
def show(self): from IPython.display import display if self.tool_bar_state: self._configure_tool_bar() self.figure.display = self.plotter.show(use_ipyvtk=True, return_viewer=True) self.figure.display.layout.width = None # unlock the fixed layout display(self.figure.display) return self.scene()
def Model_prediction(dataset=None, num=1): if dataset: for image, mask in dataset.take(num): pred_mask = model.predict(image) display([image[0], mask[0], create_mask(pred_mask)]) else: display([ sample_image, sample_mask, create_mask(model.predict(sample_image[tf.newaxis, ...])) ])
def update_output_slider(self, value): self.out.clear_output(wait=True) plt.plot(self.time, self.dt[self.select.value]) plt.xlabel("t (ps)") plt.ylabel(self.select.value) plt.xlim([0, self.slider.value]) plt.grid() with self.out: display(plt.gcf()) plt.close()
def display_table_info(table_name, job_name=None, args_str="", n=10, local=False, use_all=True, html_n=0, **kwargs): A = test_anal.head_table( table_name, job_name=job_name, args_str=args_str, n=n, local=local, use_all=use_all, **kwargs ) display(A) if html_n > 0: display(HTML(A.head(html_n).to_html())) return A
def display_or_return_df(df, return_df, style_class=None, formatters=None): if return_df: return df else: # style 을 사용하기 위해서는 show_style() 을 실행한 cell이 하나 있어야 함. html = df.to_html(escape=False, classes=style_class, formatters=formatters) display(HTML(html)) return None
def display_interactive(*args): display(fig) #clear up figure so it isn't displayed at end of execution try: show._to_draw.remove(fig) except ValueError: pass show._draw_called = False matplotlib.pyplot.close(fig) return
def display_df(df, html=False, **kwargs): print "total : %s" % len(df) df = _get_df_part(df, **kwargs) # html (충분히 길고 넓은 데이터를 보여줄 수 있음, image 표현 가능) if html: display(HTML(df.to_html())) else: display(df)
def _show_thumbs(images, width=None, ncol=None, maxwidth=None, mincol=None, maxcol=None, title=None, **kw): if not images: return None nrow, ncol, width = rv.File.compute_thumb_geometry(len(images), ncol, mincol, maxcol, width, maxwidth) npix = int(rv.DPI*width) # make list of thumbnail,filename pairs filelist = [ (os.path.basename(img.fullpath), "%s/thumbnails/%d.%s"%(os.path.dirname(img.fullpath), npix,os.path.basename(img.fullpath)), img.fullpath) for img in images ] filelist.sort() # (re)generate thumbs if needed fails = 0 for _, thumb, image in filelist: if not os.path.exists(os.path.dirname(thumb)): if os.system("mkdir %s"%os.path.dirname(thumb)): fails += 1 if not os.path.exists(thumb) or os.path.getmtime(thumb) < os.path.getmtime(image): if os.system("convert -thumbnail %d %s %s"%(npix,image,thumb)): fails += 1 html = rv.renderTitle(title) + \ """<br> <table style="border: 0px; text-align: left">\n """ if fails: html += "(WARNING: %d thumbnails failed to generate, check console for errors)<br>\n"%fails for row in range(nrow): html += """<tr style="border: 0px; text-align: left">\n""" filelist_row = filelist[row*ncol:(row+1)*ncol] for _, thumb, image in filelist_row: html += """<td style="border: 0px; text-align: center">""" html += os.path.basename(image) html += "</td>\n" html += """</tr><tr style="border: 0px; text-align: left">\n""" for _, thumb, image in filelist_row: html += """<td style="border: 0px; text-align: left">""" html += "<a href=%s><img src=%s alt='?'></a>"%(image,thumb) html += "</td>\n" html += "</tr>\n" html += "</table>" display(HTML(html))
def log_progress(sequence, every=1, size=None, name='Items'): from ipywidgets import IntProgress, HTML, VBox from IPython.display import display is_iterator = False if size is None: try: size = len(sequence) except TypeError: is_iterator = True if size is not None: if every is None: if size <= 200: every = 1 else: every = int(size / 200) # every 0.5% else: assert every is not None, 'sequence is iterator, set every' if is_iterator: progress = IntProgress(min=0, max=1, value=1) progress.bar_style = 'info' else: progress = IntProgress(min=0, max=size, value=0) label = HTML() box = VBox(children=[label, progress]) display(box) index = 0 try: for index, record in enumerate(sequence, 1): if index == 1 or index % every == 0: if is_iterator: label.value = '{name}: {index} / ?'.format( name=name, index=index ) else: progress.value = index label.value = u'{name}: {index} / {size}'.format( name=name, index=index, size=size ) yield record except: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index label.value = "{name}: {index}".format( name=name, index=str(index or '?') )
def test_check_renderer_options(): # this test should pass with check_render_options(): from IPython.display import display display(None) # check that an error is appropriately raised if the test fails with pytest.raises(AssertionError): with check_render_options(foo='bar'): from IPython.display import display display(None)
def _init_js_side (): """Checks that Javascript components of radiopadre are initialized""" try: get_ipython except: return None get_ipython().magic("matplotlib inline") # load radiopadre/js/init.js and init controls initjs = os.path.join(os.path.dirname(__file__),"js","init.js") display(Javascript(open(initjs).read())) display(Javascript("document.radiopadre.init_controls('%s')"%os.environ['USER']))
def reveal_figure(b): global controls record_action('predict', b.description) controls.close() descr = describe_and_show_figure(current_figure) descr += '<br/><br/>Actual chart form:' controls = widgets.VBox( [widgets.HTML(descr), widgets.HBox(confirm_buttons)]) display(controls)
def pathfindAndDisplay(x, y, w, h, grid, pathfinder, maxItr=float("inf")): ipgrid = IPGrid(w, h, 440 / w) ipgrid.renderGrid(grid, x, y, pathfinder=pathfinder) result = pathfinder.findPath(maxItr) if(result["failed"]): print("Failed after " + str(result["iterations"]) + " iterations!") else: print("Finished after " + str(result["iterations"]) + " iterations:") ipgrid.renderGrid(grid, x, y, pathfinder=pathfinder, path=result["pathCoords"]) display(ipgrid)
def update_plot(self, value): self.out.clear_output(wait=True) omffile = self.find_omf_file() field = df.read_oommf_file(omffile) slice_index = {"x": 0, "y": 1, "z": 2}[self.select.value] point = field.mesh.pmin[ slice_index] + self.slice_slider.value * field.mesh.l[slice_index] field.plot_slice(self.select.value, point, xsize=10) with self.out: display(plt.gcf()) plt.close()
def plot_log_odds(target, feature, bins, f_range=None, M=10, figsize=(10, 3), display_head=False, normed=True): LO, base = get_log_odds(target, feature, bins, f_range=f_range, M=M, display_head=display_head) fig, axs = plt.subplots(2, 1, figsize=figsize) ax = axs[0] feature[target == 1].hist(bins=bins, alpha=0.4, color="red", normed=normed, range=f_range, ax=ax) ax = axs[0] feature[target == 0].hist(bins=bins, alpha=0.4, color="blue", normed=normed, range=f_range, ax=ax) ax.set_xlabel(feature.name) ax = axs[1] LO.plot(ax=ax) ax.set_xlabel("%s_cut" % feature.name) ax.set_ylabel(r"$log(P(good) / P(bad))$") ax.axhline(base) display(LO)
def py_oauth_dance(): _twitter=twitter.Twitter(auth=twitter.OAuth('','',CONSUMER_KEY, CONSUMET_SECRET), format='', api_version=None) oauth_token, oauth_token_secret = parse_oauth_tokens(_twitter.oauth.request_token(oauth_callback=oauth_callback)) # Se nececitan escribir estos valores en un archivo para de ahi recojer la # llamada de Twitter que es manejada del web server en /oauth_helper write_token_file(OAUTH_FILE,oauth_token, oauth_token_secret) oauth_url=('http://api.twitter.com/oauth/authorize?oauth_token='+oauth_token) #Utilizamos el browser para abrir una nueva ventana y asi accesar al web #server y obtener la autorizacion del usuario display(JS("windows.open('%s')" % oauth_url))
def _jsDisplay(self): # Workaround to have ConvertToJSON work json = ROOT.TBufferJSON.ConvertToJSON(self.canvas, 3) #print "JSON:",json # Here we could optimise the string manipulation divId = 'root_plot_' + str(self._getUID()) thisJsCode = _jsCode.format(jsCanvasWidth = _jsCanvasWidth, jsCanvasHeight = _jsCanvasHeight, jsROOTSourceDir = _jsROOTSourceDir, jsonContent=json.Data(), jsDrawOptions="", jsDivId = divId) # display is the key point of this hook display(HTML(thisJsCode))
def _show_thumbs(fits_files, width=None, ncol=None, maxwidth=None, mincol=None, maxcol=None, title=None, fs='small', **kw): if not fits_files: return None if title: display(HTML(rv.renderTitle(title))) nrow, ncol, width = rv.File.compute_thumb_geometry(len(fits_files), ncol, mincol, maxcol, width, maxwidth) fig = plt.figure(figsize=(width * ncol, width * nrow), dpi=rv.DPI) for iplot, ff in enumerate(fits_files): ax = plt.subplot(nrow, ncol, iplot + 1) ax.tick_params(labelsize=kw.get('fs_axis',fs)) ff.show(index=[0] * 10, unroll=None, filename_in_title=True, make_figure=False, fs_title='small', **kw)
def thumbs(self, **kw): display(HTML(render_refresh_button(full=self._parent and self._parent.is_updated()))) if not self: display(HTML("<p>0 files</p>")) return None kw.setdefault('title', self._title + " (%d file%s)" % (len(self), "s" if len(self) > 1 else "")) kw.setdefault('showpath', self._showpath) thumbs = getattr(self._classobj, "_show_thumbs", None) if thumbs: return thumbs(self, **kw) display(HTML("<p>%d files. Don't know how to make thumbnails for this collection.</p>" % len(self)))
def display_full(df): """ truncation view로 들어가지 않고, df를 모두 출력한다. """ with pd.option_context("display.max_rows", len(df)), pd.option_context("display.max_columns", len(df.columns)): display(df)
from IPython import display t_minus = range(10,0,-1) def lazy_iterator(name): seq = eval(name) it = iter(seq) while True: try: yield it.next() # this looks silly locally, but it will be useful for the remote version: except StopIteration: raise StopIteration lzit = lazy_iterator('t_minus') display(lzit) list(lzit)
def subdirectory_catalog (self, basename="results", dirs="dirs", root="root", **kw): return display(HTML(self._repr_html_(copy_filename=basename,copy_dirs=dirs,copy_root=root,**kw)))
def list(self, **kw): return display(HTML(self._repr_html_(**kw)))
def show_all(self,*args,**kw): display(HTML(render_refresh_button(full=self._parent and self._parent.is_updated()))) if not self: display(HTML("<p>0 files</p>")) for f in self: f.show(*args,**kw)
def PowerTraces(A,order): IX=eye(A.shape[0]) delta=series(trace((IX-t*A).inv()),t,0,order) for i in range(order): display(delta.coeff(t,i))
def load_ipython_extension(ipython): """ Load the %eups extension. IPython extension API entry point. """ # Sanity checks. Do as many of these as necessary, to make sure we catch issues # early on that may cause difficult-to-debug misbehaviors later. # # Refuse to proceed if there's no pointer to the linkfarm in the environment, # or the linkfarm doesn't exist, or the linkfarm dir isn't in LD_LIBRARY_PATH. # The most likely cause of this is not having started ipython via the # supplied 'ipython' wrapper script, or some bug in the wrapper itself. if 'IPYTHON_EUPS_LIB_LINKFARM' not in os.environ: raise Exception(textwrap.dedent("""\ It looks like IPython hasn't been started using the 'ipython' wrapper script supplied by ipython_eups. Maybe you forgot to add it to your path? Guru Meditation: IPYTHON_EUPS_LIB_LINKFARM environmental variable has not been set. The aforementioned wrapper script sets this variable before calling IPython. """).replace('\n', ' ').replace(' ', '\n\n')) linkfarm = os.environ['IPYTHON_EUPS_LIB_LINKFARM'] if not os.path.isdir(linkfarm): raise Exception(textwrap.dedent("""\ Something is amiss with how ipython_eups has been started; directories that are needed for %eups to work don't exist. Try restarting IPython and see if the problem clears up. Guru Meditation: The directory that IPYTHON_EUPS_LIB_LINKFARM environmental variable points to -- %(linkfarm)s -- does not exist. This directory is supposed to be created by the 'ipython' wrapper script. """ % { 'linkfarm': linkfarm } ).replace('\n', ' ').replace(' ', '\n\n')) if LD_LIBRARY_PATH not in os.environ: raise Exception(textwrap.dedent("""\ Something is amiss with ipython_eups. Try restarting IPython and see if the problem clears up. Guru Meditation: The %(lddir)s environment variable has not been set. This variable was supposed to be created by the 'ipython' wrapper script. You may have uncovered a bug in the wrapper script. """ % { 'lddir': LD_LIBRARY_PATH } ).replace('\n', ' ').replace(' ', '\n\n')) liblinkfarm = os.path.join(linkfarm, 'lib') if liblinkfarm not in os.environ[LD_LIBRARY_PATH].split(':'): print os.environ[LD_LIBRARY_PATH].split(':') raise Exception(textwrap.dedent("""\ Something is amiss with ipython_eups. Try restarting IPython and see if the problem clears up. Guru Meditation: The "library linkfarm" directory exists [== %(linkfarm)s], but isn't on %(lddir)s [== %(ldpath)s]. It was supposed to be added to it by the 'ipython' wrapper script. This may be a bug in the wrapper script. """ % { 'lddir': LD_LIBRARY_PATH, 'linkfarm' : liblinkfarm, 'ldpath' : os.environ[LD_LIBRARY_PATH] } ).replace('\n', ' ').replace(' ', '\n\n')) # Re-initialize the link farm every time IPython is started, so that the environment # is properly cleaned of any left-overs from prior runs. E.g., this may happen # when IPython notebook kernel is restarted by the user. rebuild_ld_library_path_linkfarm(os.environ[LD_LIBRARY_PATH]) #print "-----------------------------------------------------------------------" #print "Note to readers: if you don't have ipython_eups installed, get it from" #print " http://github.com/mjuric/ipython_eups to enable eups_magic." #print "-----------------------------------------------------------------------" # Register %eups magic ipython.register_magic_function(eups) display("``%eups`` magics loaded. Documentation and source at http://github.com/mjuric/ipython_eups.") # Check if EUPS has been setup, if not, try to set it up # from the default environment. if 'EUPS_DIR' not in os.environ: if os.path.isdir(EUPS_DIR_DEFAULT): init_eups() else: warn(("EUPS not initialized, and no default EUPS has been symlinked as %s. " + "Make sure to initialize it with `%%eups init <path_to_eups>'") % EUPS_DIR_DEFAULT)
def eups(line): """\ Setup and unsetup EUPS products from within IPython Common usage: # Set up a product %eups setup <product> # Unsetup a product %eups unsetup <product> If no EUPS has been setup (i.e., you didn't source setups.sh before starting IPython), the default EUPS will be set up once this extension is loaded with %load_ext. You can also set it up manually with %eups init (see below). More examples: # Initialize EUPS %eups init [[--set-default] path_to_eups [EUPS_PATH]] # Version of ipython_eups script %eups version # EUPS subcommands are passed on to EUPS %eups list %eups --version More documentation on http://github.com/mjuric/ipython_eups. """ split = line.split() cmd, args = split[0], split[1:] if cmd == "unsetup": cmd = "setup" args.insert(0, '--unsetup') unsetup = True else: unsetup = False if cmd == "init": # find the '--set-default' flag set_default = '--set-default' in args if set_default: args.remove('--set-default') return init_eups(*args, set_default=set_default) elif cmd == "setup": # run 'setup' args2 = [ arg for arg in args if not arg.startswith('-') ] product = args2[0] if args2 else None # If this is an unsetup call, remember the version for printing at the end if unsetup: version = _get_setuped_product_version(product) setupimpl = os.path.join(os.environ['EUPS_DIR'], 'bin/eups_setup_impl.py') setupcmd = "python '%s' %s" % (setupimpl, ' '.join(args)) try: ret = subprocess.check_output(setupcmd, stderr=subprocess.STDOUT, shell=True).strip() except subprocess.CalledProcessError as e: error("%s\n%s" % (str(e), e.output)) else: if product is None: # If the user ran something like 'setup -h', just print out the result print ret else: ret = ret.split('\n') if not len(ret) or ret[-1] == 'false': # setup failed; return the error message error(str('\n'.join(ret[:-1]))) else: exportRe = re.compile('export\s+(\w+)=(.*?);?$') unsetRe = re.compile('unset\s+(\w+);?$') export = collections.OrderedDict() unset = set() for line in ret[:-1]: m = exportRe.match(line) if m: export[m.group(1)] = m.group(2) continue m = unsetRe.match(line) if m: unset.add(m.group(1)) continue error("EUPS told me to '%s' but I don't know how to handle that. Aborting." % (line)) # do the variable (un)setups. We do this only after # the entire output is processed above, to avoid # aborting half-way in case of errors (and leaving # the environment in an incoherent state). for var, value in export.iteritems(): value = subprocess.check_output('/bin/echo -n %s' % value, shell=True) if var == "PYTHONPATH": update_sys_path(value, os.environ[var]) if var == LD_LIBRARY_PATH: rebuild_ld_library_path_linkfarm(value) os.environ[var] = value #print "set: %s=[%s]" % (var, os.environ[var]) for var in unset: #print "unset: " + var del os.environ[var] if not unsetup: version = _get_setuped_product_version(product) # print out some useful info about what we've just setup-ed msg = "**``%%eups %s:``** `%s`, version `%s`" % ('setup' if not unsetup else 'unsetup', product, version) display(msg) elif cmd == "version": msg = "**``%%eups version:``** `%s`" % (__version__) display(msg) else: setupcmd = "eups %s %s" % (cmd, ' '.join(args)) IPython.utils.process.system(setupcmd)
def init_eups(eups_loc='default', eups_path=None, set_default=False): """ If the user forgot to run setups.sh, initialize EUPS from scratch. Do it by running setups.sh and replacing our current environment with the environment it will generate. Arguments: eups_loc: directory where EUPS is installed (where bin/setups.sh will be found). If not given, a default from ~/.eups/default will be assumed eups_path: The location of the EUPS product stack (EUPS_PATH environment variable). Optional. set_default: If set, eups_loc will be symlinked to ~/.eups/default. """ # Locate EUPS if eups_loc == 'default': if not os.path.isdir(EUPS_DIR_DEFAULT): error(textwrap.dedent("""\ You're trying to initialize EUPS without specifying its location, and the default in %(default)s does not exist (or is inaccessible). Either rerun the command as '%%eups init <eups_location>', or symlink the path to EUPS (the location where 'bin/setups.sh' can be found) as %(default)s . """ % { 'default': EUPS_DIR_DEFAULT } ).replace('\n', ' ').replace(' ', '\n\n')) return else: eups_loc = EUPS_DIR_DEFAULT # Verify that EUPS exists setups_sh = os.path.join(eups_loc, 'bin', 'setups.sh') if not os.path.isfile(setups_sh): error(textwrap.dedent("""\ Cannot find %s. Are you sure that EUPS has been installed in %s? """ % (setups_sh, eups_loc) ).replace('\n', ' ').replace(' ', '\n\n')) return try: # Load the environment cmd = r""" source %(setups_sh)s >/dev/null %(python)s -c ' import os, sys; for k, v in os.environ.iteritems(): sys.stdout.write("%%s\0%%s\0" %% (k, v)) '""" % { 'python' : sys.executable, 'setups_sh' : setups_sh } envl = subprocess.check_output(cmd, shell=True).split('\0') env = { k: v for k, v in zip(envl[::2], envl[1::2]) } os.environ.clear() os.environ.update(dict=env) # Override EUPS_PATH, if requested if eups_path is not None: os.environ['EUPS_PATH'] = eups_path finally: # Why do this in a finally clause? # a) we want to print out the messages about eups_loc and # the message about EUPS_PATH in the same Markdown block, # so they look nice on the screen. # b) but we also want the message about eups_loc to be printed # no matter what, so the user is given information about where # eups is being loaded from in case anything went wrong. msg = "**``%%eups init:``** loading EUPS from `%s`." % eups_loc if 'EUPS_PATH' in os.environ: msg += " \n**``%%eups init:``** using `EUPS_PATH=%s`." % os.environ['EUPS_PATH'] display(msg) if eups_loc != EUPS_DIR_DEFAULT: if set_default: if os.path.islink(EUPS_DIR_DEFAULT): os.unlink(EUPS_DIR_DEFAULT) msg="**``%%eups init:``** symlinking `%s` -> `%s`" % (EUPS_DIR_DEFAULT, eups_loc) display(msg) os.symlink(eups_loc, EUPS_DIR_DEFAULT) elif not os.path.islink(EUPS_DIR_DEFAULT): display("**``%%eups init:``** To make this default, rerun with ``%%eups init --set-default %s``" % eups_loc)
def show(self, width=None, **kw): display(Image(self.fullpath, width=width and width * 100))
def unprotect (): """Makes current notebook unprotected.""" display(Javascript("document.radiopadre.unprotect()")) display(HTML(render_status_message("""This notebook is now unprotected. All users can treat it as read-write.""")))
def show(self): display(self._widget)
def sympy_latex(self, line): from sympy import latex from IPython.display import Latex, display ip = get_ipython() latex = Latex("$${}$$".format(latex(ip.ev(line)))) display(latex)
def SymmTraces(A,order): IX=eye(A.shape[0]) delta=series(((IX-t*A).det())**(-1),t,0,order) for i in range(order): display(delta.coeff(t,i))