Skip to content

Instantly share code, notes, and snippets.

View travis23's full-sized avatar

Travis Niederhauser travis23

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / RunOnLoad.html
Last active September 9, 2020 15:30
[On Loading Page Run Javascript] #html #javascript #onload
<head><script>
function initialize()
/* Initialize the web page */
{
/* Do stuff */
}
</script></head>
<body onload="initialize()">