def img(self, src, *, text="", title=None, copy=True, ignore=False, raw=True, copy_sync=False): res = component.link( src=src, md_dir=self.path.parent, text=text, img=True, title=title, copy=copy, ignore=ignore, new_line=True, ) if raw: display_res = component.link( src=src, md_dir=".", text=text, title=title, copy=False, ignore=True, copy_sync=copy_sync, ) display_markdown(display_res, raw=raw) return self._save(res)
def _run_markdown(self, ns, env: "Env"): try: from IPython.display import display_markdown except ImportError: display_markdown = lambda code, raw: print(code) source = "\n".join(line[2:] for line in self.source.splitlines()) display_markdown(source, raw=True)
def md(*args): s = '' for x in args: if (isinstance(x, sp.Basic) or isinstance(x, sp.MutableDenseMatrix) or isinstance(x, tuple)): s = s + sp.latex(x) elif (isinstance(x, str)): s = s + x elif (isinstance(x, int) or isinstance(x, float)): s = s + str(x) else: print(type(x)) Display.display_markdown(s, raw=True)
def step_start(self, exp, step): title = step.replace('_', ' ').title() display_markdown(f'## {title}', raw=True) params = exp.get_step(step).get_params() data = self.params_to_display(params) if data is not None: display_markdown('### Initliazed parameters', raw=True) display(data, display_id=f'input_{step}')
def display_mds(*strings): """ Utility function to display several strings formatted in markdown format :param strings: any number of strings with text in markdown format :return: None """ for string in strings: display_markdown(Markdown(data=string))
def md(*args): s = '' for x in args: if (isinstance(x, sp.Basic) or isinstance(x, sp.MutableDenseMatrix) or isinstance(x, tuple) or isinstance(x, np.ndarray)): s = s + sp.latex(x) elif (isinstance(x, str)): s = s + x elif (isinstance(x, int) or isinstance(x, float)): s = s + str(x) else: print(type(x)) Display.display_markdown(s, raw=True)
def write_details(self, short: bool = False, keys_limit: int = 10) -> None: for result in self.results.values(): if result.detailed_messages_count: display_markdown( f"{result.name} ({result.detailed_messages_count} message(s)):" ) self.write_rule_details(result, short, keys_limit) for f in result.figures: f.show() display_markdown("<br>")
def printmk(*args, **kwargs): """Print markdown in ipython. """ if not args: return fmt, args = args[0], args[1:] from IPython.display import display_markdown, Markdown display_markdown(Markdown(fmt.format(*args, **kwargs)))
def display_params(self, index, in_shell=False): display_markdown(f"#### Params: {index}", raw=True) params = (pd.Series( self._load_artifact(artifact_type="params", index=index)).rename("params").to_frame()) if in_shell is True: print(tabulate(params, headers="keys", tablefmt="psql")) else: display(params)
def print_model_score(model, x_train, y_train, x_test, y_test): training_score = matthews_corrcoef(model.predict(x_train), y_train) test_score = matthews_corrcoef(model.predict(x_test), y_test) display_markdown( '**Matthews correlation coeff., training set: {}**'.format( np.round(training_score, 2)), raw=True) display_markdown('**Matthews correlation coeff., test set: {}**'.format( np.round(test_score, 2)), raw=True)
def display(self, in_shell=False): self.display_metrics(in_shell=in_shell) metrics = self._build_metrics() best_index = metrics["total_return"].sort_values( ascending=False).index[0] display_markdown(f"### [+] Best index: {best_index}", raw=True) display(metrics.loc[best_index]) self.display_params(index=best_index, in_shell=in_shell) self.display_report(index=best_index, in_shell=in_shell)
def experiment_start(self, exp): self.exp = exp self.steps = OrderedDict() self.running = True display_markdown('### Input Data', raw=True) X_train, y_train, X_test, X_eval, y_eval = \ exp.X_train, exp.y_train, exp.X_test, exp.X_eval, exp.y_eval tb = get_tool_box(X_train, y_train, X_test, X_eval, y_eval) display_data = (tb.get_shape(X_train), tb.get_shape(y_train), tb.get_shape(X_eval, allow_none=True), tb.get_shape(y_eval, allow_none=True), tb.get_shape(X_test, allow_none=True), exp.task if exp.task == const.TASK_REGRESSION else f'{exp.task}({tb.to_local(y_train.nunique())[0]})') display(pd.DataFrame([display_data], columns=[ 'X_train.shape', 'y_train.shape', 'X_eval.shape', 'y_eval.shape', 'X_test.shape', 'Task', ]), display_id='output_intput') try: import seaborn as sns import matplotlib.pyplot as plt from sklearn.preprocessing import LabelEncoder if exp.task == const.TASK_REGRESSION: # Draw Plot plt.figure(figsize=(8, 4), dpi=80) sns.kdeplot(y_train.dropna(), shade=True, color="g", label="Proba", alpha=.7, bw_adjust=0.01) else: le = LabelEncoder() y = le.fit_transform(y_train.dropna()) # Draw Plot plt.figure(figsize=(8, 4), dpi=80) sns.distplot(y, kde=False, color="g", label="y") # Decoration plt.title('Distribution of y', fontsize=22) plt.legend() plt.show() except: pass
def step_end(self, exp, step, output, elapsed): super().step_end(exp, step, output, elapsed) fitted_params = exp.get_step(step).get_fitted_params() importances = fitted_params.get('importances') if importances is not None: df = pd.DataFrame(zip(importances['columns'], importances['importances_mean'], importances['importances_std']), columns=['feature', 'importance', 'std']) display_markdown('### Permutation importances', raw=True) display(df, display_id=f'output_{step}_importances')
def display_report(self, report): display_markdown(f"#### Report: {self.base_currency}", raw=True) _, ax = plt.subplots(4, 1, figsize=(12, 12), sharex=True) for idx, column in enumerate(["capital", "cache", "return", "trade_return"]): if column == "trade_return": report[column].dropna().apply(lambda x: sum(x)).plot(ax=ax[idx]) else: report[column].plot(ax=ax[idx]) ax[idx].set_title(f"historical {column}") plt.tight_layout() plt.show()
def rec(self, data, *, h=None, title=None, raw=True): res = "" if any(list(map(lambda t: isinstance(data, t), [DataFrame, Series]))): default_h = h or 2 res += component.table(data, h=default_h, title=title) elif isinstance(data, Iterable) and (not isinstance( data, str)): # except type of str res += component.enum(data) else: res += component.header(data, h=h) quote = self.generate_quote() res = "".join([quote + line for line in res.splitlines(keepends=True)]) display_markdown(res, raw=raw) return self._save(res)
def _display(df: pyspark.sql.DataFrame, num_rows: int = 5, truncate: bool = False, mimetype: str = 'text/html') -> (): """ Invoke IPython `display` with specific controls. :param num_rows: number of rows to render :param truncate: If `True`, shorten width of columns to no more than 40 characters :return: None """ if "html" in mimetype: display_html(spark_df_to_html(df, num_rows, truncate), raw=True) else: display_markdown(spark_df_to_markdown(df, num_rows, truncate), raw=True)
def display_json(builder, indent=2): """Pretty print a dictionary object in a Jupyter Notebook.""" from IPython.display import display_markdown return display_markdown( "```json\n{}\n```".format( json.dumps(builder, cls=BuilderEncoder, indent=indent)), raw=True, )
def step_end(self, exp, step, output, elapsed): super().step_end(exp, step, output, elapsed) fitted_params = exp.get_step(step).get_fitted_params() input_features = fitted_params.get('input_features') selected = fitted_params.get('selected_features') if selected is None: selected = input_features importances = fitted_params.get('importances', []) is_selected = [ input_features[i] in selected for i in range(len(importances)) ] df = pd.DataFrame(zip(input_features, importances, is_selected), columns=['feature', 'importance', 'selected']) df = df.sort_values('importance', axis=0, ascending=False) display_markdown('### Feature importances', raw=True) display(df, display_id=f'output_{step}_importances')
def write_detailed_errors(cls, errors: Dict, short: bool, keys_limit: int) -> None: error_messages = sorted(errors.items(), key=lambda i: len(i[1]), reverse=True) if short: keys_limit = 5 error_messages = error_messages[:5] for attribute, keys in error_messages: if isinstance(keys, list): keys = pd.Series(keys) if isinstance(keys, set): keys = pd.Series(list(keys)) sample = Report.sample_keys(keys, keys_limit) display_markdown( f"{len(keys)} items affected - {attribute}: {sample}")
def print_events(self): week = 0 module = 0 lastday = 8 mdtext = 'Welcome to Molecular Spectroscopy!\r\rThe schedule below will be updated regularly with links to online materials for each lecture. Dark blue text indicates a Jupyter Notebook-based programming/simulation module, while light-blue headings link to PDF-format lecture notes. Holidays are indicated in purple. Underlining indicates that the link is active and ready for use.\r\rPlease note! You\'ll need to be logged into your Brightspace account to view the video lectures. (Videos are actually hosted on Brightspace, although links are provided here.)\r\rClick [here](git/CHM676/src/CHM676_Syllabus_2020.pdf) for the course syllabus. ([Here](https://983291-6.kaf.kaltura.com/media/t/0_cwi5kkdu/177251882) for a video walk-through.)\r\r**Information on the [Final Project](git/CHM676/src/CHM676_Final_Project.pdf) is now posted**.' for item in self.events: if item.isnew: module += 1 mdtext += "\r" mdtext += '## Module ' + str(module) + " \r" if item.date.weekday() < lastday: week += 1 mdtext += '#### Week ' + str(week) + "\r" lastday = item.date.weekday() datetext = " * " + item.date.strftime("%b %-d") + ": " holiday_color = 'Purple' lecture_color = '#3090C7' compute_color = 'DarkBlue' if item.cformat == 'holiday': titlecol = holiday_color elif item.cformat == 'lecture': titlecol = lecture_color elif item.cformat == 'compute': titlecol = compute_color else: titletext = item.title if len(item.link) > 0: titletext = "<a href=\"" + item.link + "\"> <span style=\"color:" + titlecol + ";text-decoration:underline\">" + item.title + "</span></a>" else: titletext = "<span style=\"color:" + titlecol + "\">" + item.title + "</span>" #titletext = '[' + titletext + '](' + item.link + ')' if len(item.vlink) > 0: titletext += ' ([video](' + item.vlink + '))' mdtext += datetext + titletext + " \r\r" display_markdown(mdtext, raw=True)
def display_report(self, index, in_shell=False): report = self._load_artifact(artifact_type="report", index=index) display_markdown(f"#### Report: {index}", raw=True) _, ax = plt.subplots(4, 1, figsize=(12, 12), sharex=True) for idx, column in enumerate( ["capital", "cache", "return", "trade_return"]): if column == "trade_return": report[column].dropna().apply(lambda x: sum(x)).plot( ax=ax[idx]) else: report[column].plot(ax=ax[idx]) ax[idx].set_title(f"historical {column}") plt.tight_layout() if in_shell is True: plt.show(block=True) else: plt.show()
def step_end(self, exp, step, output, elapsed): step_obj = exp.get_step(step) fitted_params = step_obj.get_fitted_params() data = self.fitted_params_to_display(fitted_params) if data is not None: display_markdown('### Fitted parameters', raw=True) display(data, display_id=f'output_{step}') display_markdown('### Elapsed', raw=True) display_markdown(f'* {step_obj.elapsed_seconds:.3f} seconds', raw=True)
def on_search_start(self, hyper_model, X, y, X_eval, y_eval, cv, num_folds, max_trials, dataset_id, trial_store, **fit_kwargs): self.start_time = time.time() self.max_trials = max_trials df_holder = pd.DataFrame() settings = { 'X': X.shape, 'y': y.shape, 'X_eval': X_eval.shape if X_eval is not None else None, 'y_eval': y_eval.shape if y_eval is not None else None, 'cv': cv, 'num_folds': num_folds, 'max_trials': max_trials, # 'dataset_id': dataset_id, # 'trail_store': trial_store, 'fit_kwargs': fit_kwargs.keys() } df_settings = pd.DataFrame({k: [v] for k, v in settings.items()}) display_markdown('#### Experiment Settings:', raw=True) display(hyper_model, display_id=False) display(df_settings, display_id=False) display_markdown('#### Trials Summary:', raw=True) handle = display(df_holder, display_id=True) if handle is not None: self.search_summary_display_id = handle.display_id display_markdown('#### Best Trial:', raw=True) handle = display(df_holder, display_id=True) if handle is not None: self.best_trial_display_id = handle.display_id handle = display({'text/markdown': '#### Current Trial:'}, raw=True, include=['text/markdown'], display_id=True) if handle is not None: self.title_display_id = handle.display_id handle = display(df_holder, display_id=True) if handle is not None: self.current_trial_display_id = handle.display_id
def document(name): for line in _Document_[name]: Display.display_markdown(''.join(line), raw=True)
def write_rule_name(rule_name: str) -> None: display_markdown(f"{rule_name}:")
def display_performance(self): data_dict = self._load_data_dict() display_markdown("#### Timeseries", raw=True) self._display_timeseries(data_dict=data_dict) display_markdown("#### Total Performance", raw=True) total_performance = self._build_total_performance(data_dict=data_dict) display( ft.display(total_performance.rename("bin_acc").to_frame().T, axis=1)) # Build levels label_levels = self._build_levels(data=data_dict["labels"]) prediction_levels = self._build_levels(data=data_dict["predictions"]) abs_prediction_levels = self._build_levels( data=data_dict["predictions"].abs()) probability_levels = self._build_levels( data=data_dict["probabilities"]) display_markdown("#### Performance on label levels", raw=True) display( ft.display( self._build_performance_on_levels(data_dict=data_dict, levels=label_levels))) display_markdown("#### Performance on prediction levels", raw=True) display( ft.display( self._build_performance_on_levels(data_dict=data_dict, levels=prediction_levels))) display_markdown("#### Performance on abs(prediction) levels", raw=True) display( ft.display( self._build_performance_on_levels( data_dict=data_dict, levels=abs_prediction_levels))) display_markdown("#### Performance on probability levels", raw=True) display( ft.display( self._build_performance_on_levels(data_dict=data_dict, levels=probability_levels)))
import os import re import pickle import sympy import numpy as np import scipy as sp import plotly import plotly.offline as py import plotly.graph_objs as go import plotly.figure_factory as ff from plotly.offline import download_plotlyjs, init_notebook_mode from IPython.display import display, display_markdown, Image sympy.init_printing() init_notebook_mode(connected=True) show = lambda s: display_markdown(s, raw=True)
#%% Load Dependencies from IPython.display import display_markdown from pyvlm import LatticeResult from pyvlm import latticesystem_from_json #%% Create Lattice System jsonfilepath = '../files/Test_taper.json' lsys = latticesystem_from_json(jsonfilepath) display_markdown(lsys) #%% Original Strip Geometry display_markdown(lsys.strip_geometry) #%% Original Strip Geometry display_markdown(lsys.panel_geometry) #%% Original Case lres_org = LatticeResult('Baseline', lsys) lres_org.set_state(alpha=3.0) #%% Original Strip Forces display_markdown(lres_org.strip_forces) #%% Original Strip Coefficients display_markdown(lres_org.strip_coefficients) #%% Original Panel Forces display_markdown(lres_org.panel_forces) #%% Print Result display_markdown(lres_org)
#%% # Import Dependencies from IPython.display import display_markdown from pyapm import panelsystem_from_json from pyapm.classes.horseshoevortex2d import HorseshoeVortex2D, Vector2D from pyapm.outputs.msh import panelresult_to_msh from matplotlib.pyplot import figure from pygeom.matrix2d import zero_matrix_vector, elementwise_dot_product from numpy.matlib import zeros #%% # Create Panel Mesh jsonfilepath = '../files/Prandtl-D2.json' psys = panelsystem_from_json(jsonfilepath) display_markdown(psys) #%% # System Plots axt1 = psys.plot_twist_distribution() _ = axt1.set_ylabel('Twist (deg)') _ = axt1.set_xlabel('Span-Wise Coordinate - b (m)') axt2 = psys.plot_tilt_distribution() _ = axt2.set_ylabel('Tilt (deg)') _ = axt2.set_xlabel('Span-Wise Coordinate - b (m)') axt = psys.plot_chord_distribution() _ = axt.set_ylabel('Chord (m)') _ = axt.set_xlabel('Span-Wise Coordinate - b (m)') # axw = psys.plot_strip_width_distribution() # _ = axw.set_ylabel('Strip Width (m)') # _ = axw.set_xlabel('Span-Wise Coordinate - b (m)')
def _dmf_markdown(text): display_markdown(text, raw=True)
#%% # Load Dependencies from IPython.display import display_markdown from pyapm import panelsystem_from_json from pyapm.outputs.msh import panelresult_to_msh from pyvlm import latticesystem_from_json #%% # Create Panel System jsonfilepath = '../files/Test_straight.json' psys = panelsystem_from_json(jsonfilepath) display_markdown(psys) #%% # Create Lattice System jsonfilepath = '../files/Test_straight.json' lsys = latticesystem_from_json(jsonfilepath) display_markdown(lsys) #%% # Panel Result pres = psys.results['Test Alpha'] display_markdown(pres) #%% # Lattice Result Result lres = lsys.results['Test Alpha'] display_markdown(lres) #%% # Plot Lift Distribution
#%% # Import Dependencies from IPython.display import display_markdown from pyapm import panelsystem_from_json from pyapm.outputs.msh import panelresult_to_msh #%% # Import Geometry jsonfilepath = '../files/Aircraft.json' psys = panelsystem_from_json(jsonfilepath) #%% # Display System display_markdown(psys) #%% # Display Results for case in psys.results: pres = psys.results[case] display_markdown(pres) #%% # Mesh File Output ptrm = psys.results['Positive 1g Cruise'] panelresult_to_msh(ptrm, '../results/Aircraft.msh') display_markdown(ptrm) #%% # Deflect Elevator pres = ptrm.to_result('Positive 1g Cruise + Controls 25 deg') pres.set_controls(elevator=25.0, aileron=25.0, rudder=25.0)