Skip to content

Instantly share code, notes, and snippets.

@estysdesu
estysdesu / chsh.sh
Last active July 21, 2019 04:53
[Shell: add a new shell and change default] #chsh #sh
# add a new default shell
sudo vim /etc/shells
chsh -s <path to shell>
@estysdesu
estysdesu / proxy.conf
Last active July 21, 2019 05:54
[Shell/Apt: proxies] #proxy #sh #apt #debian #ubuntu
##### Ubuntu/Debian Apt #####
# /etc/apt/apt.conf.d/proxy.conf
# https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-set-the-proxy-for-apt-for-ubuntu-18-04/
Acquire {
HTTP::proxy "<ip-addr>";
HTTPS::proxy "<ip-addr>";
}
@estysdesu
estysdesu / which.py
Last active April 5, 2020 13:03
[Python: Check Existence in PATH] #shell #shutil #subprocess #path #which #python
#!/usr/bin/env python3
import shutil, subprocess
cmd = shutil.which("python3")
try:
runner = subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e: # python3 is not found in path
print(str(e))
exit(1)
@estysdesu
estysdesu / window.py
Last active July 21, 2019 05:51
[Python: windows of an iterable with repeating edge case condition] #window #python
#!/usr/bin/env python3
def window(data, w_size):
""" Get all windows for a list with an odd window size. """
if not getattr(data, '__iter__', False):
raise ValueError("data must be an iterable")
if w_size%2 != 1:
raise ValueError("window size must be odd")
if w_size > len(data):
raise ValueError("window size must be less than the data size")
@estysdesu
estysdesu / sig_x_corr.py
Last active July 21, 2019 05:02
[Python: signal cross correlation (signal/phase comparison)] #cross #correlation #signals #numpy #xcorr #np
import numpy as np
t = np.linspace(0, 4*np.pi, 100)
y1 = np.sin(t)
randomizer = lambda x: x + np.random.choice([-1, 1])*np.random.rand()
# y2 = np.apply_along_axis(randomizer, 0, y1) # applies once on whole array instead of per element of array
vRandomizer = np.vectorize(randomizer)
y2 = vRandomizer(y1)
corr = np.corrcoef(y1, y2)[0, 1] # choose a value from the left --> right diagonal; either [0, 1] or [1, 0]
@estysdesu
estysdesu / ma.py
Last active July 21, 2019 05:02
[Python: Numpy mask arrays] #numpy #maskarray #python #ma #np
import numpy as np
x1 = np.ma.array([0, 1, 2, 3, 4])
x1.mask # --> [False]
x2 = np.ma.array([0, 1, 2, 3, 4], mask=[False]) # triggers mask expansion
x2.mask # --> [False, False, False, False, False]
@estysdesu
estysdesu / drop_tracker.py
Last active July 21, 2019 05:03
[Python: track values that are removed from an origin array] #maskedarray #numpy #np #ma
import numpy as np
def drop_tracker(curArr, rmIndxs, dropTracker):
"""
Track values that are removed from an array.
dropTracker is a MaskedArray that curArr has derived from and slightly changed.
Requires curArr pts to be closer to their complimentary dropTracker pts more than any other dropTracker pts.
"""
assert type(dropTracker) is np.ma.MaskedArray
newArr = np.delete(curArr, rmIndxs)
@estysdesu
estysdesu / bindepend.py
Last active July 21, 2019 05:51
[PyInstaller: enable in virtual environment] patch file for PyInstaller in a venv #pyinstaller #venv #virtual #env #python
# PyInstaller/bindepend.py
#-----------------------------------------------------------------------------
# Copyright (c) 2013-2018, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
@estysdesu
estysdesu / grad_n.py
Last active July 21, 2019 05:02
[Python: Numpy n-th gradient (recursion)] #numpy #gradient #slope #recursion #np
import numpy as np
def grad_n(y, x, n, edge_order=2):
g = np.gradient(y, x, edge_order=edge_order)
for _ in range(n-1):
g = grad_n(g, x, 1, edge_order=edge_order)
return g
@estysdesu
estysdesu / med_filt.py
Last active July 21, 2019 05:51
[Python: median filter (1d)] #median #filter #medfilt #window #python
#!/usr/bin/env python3
import window # https://gist.github.com/estysdesu/af7301fa17817c0d0ac7b5f8952c9bfe
def median_filt_1d(data, w_size):
""" Median filter for 1d data. Edges are handled with a window shift. """
windowed_data = window(data, w_size)
w_half_size = w_size//2
for i, w_data in enumerate(windowed_data):
w_data_sort = sorted(w_data)