Skip to content

Instantly share code, notes, and snippets.

View travis23's full-sized avatar

Travis Niederhauser travis23

View GitHub Profile
@travis23
travis23 / OpenJupyterLab.bat
Created April 15, 2020 15:56
[Batch File to Open Jupyter Lab] #batch #Jupyter #Anaconda
set root=C:\Users\user\Anaconda3
call %root%\Scripts\activate.bat %root%
call activate py37
call cd "C:\Users\TNiederhauser\Dropbox (Medic)\MedicData\Floor Scale"
call jupyter lab
@travis23
travis23 / create_data_directory.py
Created January 23, 2020 20:43
[Create Data Directory with Meaningful Name] #directory #python #pendulum
import os
import pendulum
def create_data_directory(
data_kind, # 'VolCal'
data_source_id,
timezone,
parent_directory
):
@travis23
travis23 / read_csv.py
Created January 9, 2020 19:23
[Read csv with Python] #python #csv #null
# See https://stackoverflow.com/questions/7894856/line-contains-null-byte-in-csv-reader-python
with open(path_to_csv_file) as f:
reader = csv.reader((x.replace('\0', '') for x in f), delimiter=',') # Handle null values
while True:
try:
line = next(reader)
except StopIteration:
break
print(line) # process lines
@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 / ranges_in_between_ranges.py
Created September 24, 2019 15:42
[Boundaries Between Range Boundaries] #endpoints #numpy #classify #pandas
def get_ranges_in_between_ranges(df, start_name, stop_name):
"""
Get range boundaries in between range boundaries arranged to be strictly increasing.
For example if we had three range boundaries where the range start and stop values
are found in two separate columns in a dataframe,
start, stop
-----------
0, 5
7, 8
@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 / 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 / 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 / filename_pattern_matching.py
Last active June 1, 2019 23:28
[filename pattern matching] #python #filename #fnmatch
import fnmatch
pattern = "aaa_*" # Special characters '*' and '?'
list_of_all_files = ['aaa_1.json', 'a.json', 'b.json']
for filename in list_of_all_files:
if fnmatch.fnmatch(filename, pattern):
print(filename)