Files
audio-driven-face-animation/project/notebooks/notebook_tricks.ipynb
T
2020-11-20 02:35:14 +01:00

8.2 KiB

Import modules from parent directory

In [ ]:
# add directory above current directory to path
import sys
sys.path.insert(0, '..')
In [ ]:
# now we can import modules from parent directory
import train
import pipeline_modules

Define function to print object length with commas

In [ ]:
showlen = lambda data: print(f"{len(data):,}")
showlen([0 for i in range(2395940)])

Create quick progress bars with tqdm

In [ ]:
from tqdm import tqdm
import time

for i in tqdm(range(10)):
    time.sleep(1)

Prettier ipywidgets version of the progress bar specifically for the notebook environment

In [ ]:
from tqdm.auto import tqdm

for i in tqdm(range(10)):
    time.sleep(1)

Beyond basic for loops, you can also use it in list comprehensions

In [ ]:
some_list = [x**2 for x in tqdm(10*[0])]

If you’re iterating over a generator, tqdm won’t know the length and the progress bar will be more limited but if you know or can calculate the total length you can supply that as an argument

In [ ]:
# generator expressions are surrounded by parenthesis ()
generator = (x**2 for x in [1, 2, 3, 4])
# print(next(generator))

some_list = [x for x in tqdm(generator, total=4)]

Jupyter Notebook Shortcuts

  • Esc will take you into command mode where you can navigate around your notebook with arrow keys.
  • While in command mode:
    • A to insert a new cell above the current cell, B to insert a new cell below.
    • M to change the current cell to Markdown, Y to change it back to code
    • D + D (press the key twice) to delete the current cell
    • Enter will take you from command mode back into edit mode for the given cell.
  • Shift + Tab will show you the Docstring (documentation) for the the object you have just typed in a code cell – you can keep pressing this short cut to cycle through a few modes of documentation.
  • Ctrl + Shift + - will split the current cell into two from where your cursor is.
  • Esc + O Toggle cell output.
  • Select Multiple Cells:
    • Shift + J or Shift + Down selects the next sell in a downwards direction. You can also select sells in an upwards direction by using Shift + K or Shift + Up.
    • Once cells are selected, you can then delete / copy / cut / paste / run them as a batch. This is helpful when you need to move parts of a notebook.
    • You can also use Shift + M to merge multiple cells.
In [ ]:
from IPython.display import IFrame, HTML, YouTubeVideo
# YouTubeVideo("ZbrRrXiWBKc")
In [ ]:
from ipywidgets import FloatSlider, interactive, IntSlider
In [ ]:
def simple_ex(amplitude=2.0, phase=0.0):
    x = np.linspace(-2*np.pi, 2*np.pi, 1000)
    y = amplitude * np.sin(x + phase)
    plt.rcParams['figure.figsize'] = 8,6
    plt.figure()
    plt.plot(x, y)
    plt.xlim(-3, 3)
    plt.ylim(-2*np.pi, 2*np.pi)

In [ ]:
amplitude_slider = FloatSlider(value=2.0, min=-10, max=10.0, step=.05)
phase_slider = FloatSlider(value=0.0, min=-np.pi, max=np.pi, step=.05)

w = interactive(simple_ex,
                amplitude=amplitude_slider,
                phase=phase_slider
               )
In [ ]:
import pandas as  pd
data = pd.read_csv('loan_train.csv')

import pandas_profiling
report = pandas_profiling.ProfileReport(data)

report
In [ ]:
# print many outputs in the same cell!
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
In [ ]:
%history -t
In [ ]: