Skip to content

Instantly share code, notes, and snippets.

View travis23's full-sized avatar

Travis Niederhauser travis23

View GitHub Profile
@travis23
travis23 / verify_path_exists.py
Last active April 30, 2020 20:31
[Verify Path Exists] #os #path #assert #raise #python
def verify_path_exists(path_):
try:
assert os.path.exists(path_)
except (AssertionError,) as error:
print()
print('Path not found')
print(path_)
raise FileNotFoundError
@travis23
travis23 / change_tab_title.ipynb
Created August 29, 2019 23:46
[JupyterLab Change Tab Title] #jupyterlab
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@travis23
travis23 / remove_nonunique_times.py
Last active July 7, 2020 14:49
[remove points that contain non-unique time values from timeseries] #numpy #values #unique #python #strictly_increasing #timeseries
indices = np.unique(time_arr.values, return_index=True)[1]
time_strict = time_arr.values[indices]
response_strict = response_arr.values[indices]
@travis23
travis23 / sort_matched_arrays.py
Last active August 8, 2019 01:51
[sort matched arrays pairs] #python #numpy #argsort #unique #timeseries #monotonic #strictly_increasing
# Sometimes timeseries need to be concatenated and then sorted so that the time vector is monotonic.
# ---- Monotonic in x, but not strictly increasing
# ********************************************************************************
x1 = np.array([1, 2, 3, 4, 4]) # Not all of the values are unique
y1 = np.array([10, 20, 30, 42, 40])
x2 = np.array([1.5, 2.5, 3.5, 4.5])
y2 = np.array([15, 25, 35, 45])
x_ = np.concatenate((x1, x2))
@travis23
travis23 / find_index_of_first_nonzero_occurrence.py
Last active June 27, 2019 21:26
[Find Index of First Non-zero Occurrence] #numpy #nonzero
import numpy as np
def find_index_of_first_nonzero_occurrence(array):
"""
Find the index of the first non-zero occurrence in a numpy array.
This is essentially a wrapper around np.flatnonzero(). If there are
no non-zero values None is returned.
array (numpy array): Should be 1d array and all elements should be numeric
"""
@travis23
travis23 / advice.md
Last active January 13, 2023 16:20
[advice] #advice #documentation #python #import #module #package #pip #conda #sounds #json #interpolation
@travis23
travis23 / mplcursors_demo.py
Last active June 2, 2019 02:10
[matplotlib mplcursors demo] mplcursors provides a very easy way to add hover text capability to matplotlib #matplotlib #mplcursors #window_title #GridSpec #layout #tooltip #hover_text #annotation #tick_labels
import matplotlib.pyplot as plt
from matplotlib import gridspec
import mplcursors
annotations0 = ['cat', 'dog', 'hare']
x = [1, 2, 3]
y = [0, 2.5, 3]
fontsize=20
@travis23
travis23 / levels_to_step_plot.py
Last active June 30, 2020 11:58
[levels_to_step_plot] #python #plot #step_plot
import numpy as np
def levels_to_steps_post(x_vec, y_vec):
"""Provides data like matplotlib.pyplot.step with where set to 'post'"""
x_new = [None] * (len(x_vec) * 2 - 1)
y_new = [None] * (len(x_vec) * 2 - 1)
for i in range(len(x_vec)-1):
x_new[2*i] = x_vec[i]
@travis23
travis23 / logging.py
Last active October 30, 2019 18:03
[Simple Logging] #python #logging #jupyter
def create_logger(log_file_path, display_level, file_level):
"""
Logs to file as well as to display.
See https://www.dataquest.io/blog/advanced-jupyter-notebooks-tutorial/
Args:
log_file_path (str):
display_level (int): logging.INFO for example
file_level (int): logging.INFO for example
@travis23
travis23 / pytest_tmpdir_factory.py
Created January 12, 2019 01:03
[pytest tmpdir_factory] a demonstration of using a pytest temporary directory #pytest #python #directory
import json
import os
import pytest as pt
# See Python Testing with pytest. > Chapter 4 > Using tmpdir and tmpdir_factory
class TestStuff(object):
@pt.fixture(scope='class')