Created
March 1, 2019 21:01
-
-
Save laurenmarietta/b43ebda7a00d8559fcb8b733e04bfe2f to your computer and use it in GitHub Desktop.
Bash Profile Tricks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~~~~~~~~~~~~~~ Personal Settings ~~~~~~~~~~~~~~~~~~~ | |
# Open a file with Sublime Text | |
alias subl="open -a 'Sublime Text'" | |
# Set a default editor | |
# If you prefer another editor, change the export EDITOR and VISUAL lines accordingly. | |
export EDITOR='subl' | |
# export VISUAL=edit | |
# make life easier | |
alias ll='ls -l' | |
alias la='ls -a' | |
alias HELPME='subl ~/unix_tricks.txt' | |
alias HELPME_PYTHON='subl ~/python_tricks.txt' | |
alias howdoiopenthis='subl ~/open_shortcuts.py' | |
alias docstring='subl ~/exampledocstring.txt' | |
alias colors='open ~/named_colors.png' | |
alias howtotest='echo pytest ./jwql/tests/ --self-contained-html --cov=./ --cov-report=html' | |
# Check that your environment is okay before starting ipython | |
# alias ipython='bash ~/ipython_env_check.sh' | |
ipython() { | |
env=$( conda list ) | |
if [[ $env != *"ipython"* ]]; then | |
echo "**************************************************************************** | |
iPython not installed in current environment. Please switch environments or | |
install iPython. | |
****************************************************************************" | |
else | |
command ipython "$@" | |
fi | |
} | |
# Check that your environment is okay before starting jupyter notebook | |
jupyter() { | |
if [[ $@ == *"notebook"* ]]; then | |
bash ~/jupyternotebook_env_check.sh | |
else | |
command jupyter "$@" | |
fi | |
} | |
# terminal window colors | |
alias ls='ls -G' | |
export CLICOLOR=1 | |
export LSCOLORS=ExfxcxdxbxaxbxcxdxExFx | |
export LS_COLORS="${LS_COLORS}:*.txt=00;36:*.py=00;32:" | |
#define the bash prompt | |
# export PS1='...$(__git_ps1)> ' | |
export PS1='\w ...> ' | |
# ALWAYS start in Astroconda (Python 3) | |
# source activate astroconda |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# Bash script to check that you can launch jupyter notebook without | |
# problems. | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# Lauren Chambers | |
# March 1, 2019 | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
env=$( conda list ) | |
if [[ $env != *"notebook"* ]]; then | |
echo "**************************************************************************** | |
Notebook not installed in current environment. Please switch environments or | |
install notebook. | |
****************************************************************************" | |
fi | |
if [[ $env != *"jupyter"* ]]; then | |
echo "**************************************************************************** | |
Jupyter not installed in current environment. Please switch environments or | |
install Jupyter. | |
****************************************************************************" | |
fi | |
if [[ $env = *"notebook"* ]] && [[ $env = *"jupyter"* ]]; then | |
jupyter notebook | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#~~~~~~~~~~~~~~~~~~~~ PYTHON DEFAULT ~~~~~~~~~~~~~~~~~~~ | |
f = open(file) | |
f.write(~~~) | |
f.read() # ’r’ = reading, ,’w’ = writing, ,’a’ = appending | |
lines = f.readlines() | |
f.close() | |
# or | |
with open(file) as f: | |
lines = f.readlines() | |
#(closing happens automatically; can add an if statement) | |
#~~~~~~~~~~~~~~~~~~~~ CSV ~~~~~~~~~~~~~~~~~~~ | |
np.genfromtext() | |
#~~~~~~~~~~~~~~~~~~~~ FITS ~~~~~~~~~~~~~~~~~~~ | |
from astropy.io import fits | |
infile = 'icft01crq_raw.fits' | |
fits.info(infile) | |
hdulist = fits.open(infile) | |
hdr = hdulist[0].header # Get the primary header | |
data = hdulist[1].data # Get the data from the 1st extension | |
# or | |
f = fits.open('ibsa01fpq_flt.fits') | |
data = f['sci'].data | |
f.close() | |
plt.imshow(data, vmin=3, vmax=20, origin='lower') # Can fiddle with vmin and vmax | |
# or | |
hdu = fits.open(infile) | |
hdu.data | |
hdu.close() # Manually close the file. | |
# or | |
with fits.open(infile) as hdu: | |
# Do something to the hdu | |
hdu.data | |
#(closing happens automatically; can add an if statement) | |
# WRITE: ~~~~~~~~~~~~~~~~~~~ | |
hdu = fits.PrimaryHDU(array) | |
hdu.writeto('name.fits') | |
#~~~~~~~~~~~~~~~~~~~~ ASCII ~~~~~~~~~~~~~~~~~~~ | |
from astropy.io import ascii as asc | |
infile = 'flux_vs_time_A.dat' | |
data = asc.read(infile, names=[~~~]) # list of column names | |
# WRITE: ~~~~~~~~~~~~~~~~~~~ | |
asc.write(table, output=filename) | |
#~~~~~~~~~~~~~~~~~~~~ EXCEL ~~~~~~~~~~~~~~~~~~~ | |
import pandas as pd | |
pd.read_excel(~~~) | |
#~~~~~~~~~~~~~~~~~~~~ YAML ~~~~~~~~~~~~~~~~~~~ | |
import yaml | |
with open(filename, encoding="utf-8") as f: | |
yaml_dict = yaml.load(f.read()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When in doubt: help(~~~~) | |
A really good guide: | |
https://github.com/jakevdp/2014_fall_ASTR599/tree/master/notebooks | |
TESTING: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
To generate a coverage report: | |
pytest ./tests/test_psfgrid.py --self-contained-html --cov=./ --cov-report=html | |
IPYTHON: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
To not have to reload modules in ipython: | |
%load_ext autoreload | |
@autoreload 2 | |
(https://ipython.org/ipython-doc/3/config/extensions/autoreload.html) | |
FILES: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
wondering how to open something? ‘howdoiopenthis’ | |
To run in Terminal as stand-alone: | |
chmod +x ~~~.py | |
in file: “#!/usr/bin/env python” | |
Return current line of script: | |
from inspect import currentframe, getframeinfo | |
getframeinfo(currentframe()).lineno) | |
Exit and report line number in file: | |
sys.exit('Hey, you stopped me at line {}.'.format(getframeinfo(currentframe()).lineno)) | |
Example of Documentation String: | |
https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt#docstring-standard | |
FILE OPERATIONS: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
To copy: | |
shutil.copyfile(from, to) | |
OBJECTS: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
**dir(var) returns ALL OF THE THINGS YOU CAN DO TO THE VARIABLE** | |
object.attribute | |
object.method() | |
type(instance) = object type of that instance | |
function(object) | |
DATA: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
tuple = can’t change/redefine/add elements | |
NUMPY CUBES: (z, y, x) (IDL x, y, z) | |
LISTS: | |
[1,2].append([3,4]) —> [1,2, [3,4]] | |
[1,2].extend([3,4]) -> [1,2,3,4] | |
[1,2,3].pop() -> list becomes [1,2]; returns 3 | |
[1,2,3].pop(1) -> list becomes [1,3]; returns 2 | |
[2,3,1].sort() -> [1,2,3] | |
sorted([2,3,1]) = [1,2,3] | |
del [1,2,3][-1] | |
[1,2,3][::-1] = [3,2,1] | |
[‘a’,’b’,’c’].index(‘b’) = return the index of ‘b’ | |
‘_’.join([‘a’, ‘b’, ‘c’] = ‘a_b_c’ | |
print something[0:5] = print the 0th, 1st, 2nd, 3rd, and 4th elements | |
print something[0:5:2] = print the 0th, 2nd, and 4th elements (steps of 2) | |
DICTIONARIES: | |
d.keys() {or list(d.keys()),in py 3} | |
d.values() | |
NUMBERS: | |
1/3 will give you 0!!!! Did you remember that?? | |
0.1 + 0.2 == 0.3 -> FALSE | |
basically, all numbers are a lie | |
you should almost never use == with numbers | |
// = FLOOR, % = remainder | |
STRINGS: | |
line.split() = splits line by whitespace | |
re.split(‘delimiter’, string) | |
for multiple delimiters, re.split(‘,|:’, string) | |
‘ ‘.join([‘hi’, ‘mark’]) —> ‘hi mark’ | |
str.replace(string, ‘a’, ‘b’) = replace ‘a’ with ‘b’ everywhere in string | |
'second: {1}, first: {0}'.format(3.5, 9.8) —> ‘second: 9.8, first: 3.5' | |
'Countrate: {:.0f} c/s'.format(obs.countrate()) -> ‘Countrate: 628 c/s’ | |
formatting strings: https://pyformat.info/ | |
ARRAYS: | |
the best thing. Use them always. (they can do exponents and stuff) | |
one data type | |
np.array(dtype=~~) = creates numpy array with all elements as ~~~ type | |
Faster to index using a[1,2] than a[1][2] | |
To copy an array, use np.copy(). (just assigning new variables won’t cut it) | |
masking: mask = [(STIS_wavelength>1450) & (STIS_wavelength <1550)] | |
np.argwhere(x==3) = return the index of 3 in the array x | |
np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]]) = array([1, 2, 3, 4, 5, 6, 7, 8, 9]) | |
FUNCTIONS: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
RANGES: | |
range(4) = creates list of numbers from 0 to 3 | |
xrange(4) = iterates 4 times | |
linspace(a, b, x) = creates list of x numbers from a to b | |
TIME: | |
now = time.time() | |
time.strftime("%Y%m%d_%H%M%S") | |
OR | |
now = datetime.datetime.now() | |
time_filename = "{:2d}{:2d}{:2d}-{:2d}{:2d}{:2d}-file.txt".format( | |
now.year, now.month, now.day, now.hour, now.minute, now.second | |
) | |
random.random() = random float in [0.0 and 1.0) | |
EMBED VIDEO: | |
from IPython.display import YouTubeVideo | |
YouTubeVideo('qJaTxaulAMo') | |
sys.stdout.write("\r~~~~”) = dynamic output | |
sys.exit(message) = system exit; tries to honor statements and can be intercepted | |
TERMINAL COMMANDS: | |
import os | |
os.system(“~~~~”) = execute ~~~ command | |
os.getcwd() = pwd | |
os.chdir() = cd | |
BINARY OPERATIONS: | |
& - binary and | |
| - binary or | |
^ - binary xor | |
ERROR HANDLING: | |
try: | |
sumfin | |
except ErrorType: | |
print(error) | |
import exceptions | |
raise exceptions.ErrorType(‘message) = will STOP it all! | |
import warnings | |
warnings.warn(‘message’, warningtype) = won’t stop everything | |
PLOTS: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
plt.style.use('prettyplots') | |
fig = plt.figure() | |
ax = fig.add_subplot() | |
plt.plot() | |
fitsplot = ax.imshow() | |
cbar = plt.colorbar(fitsplot, fraction=0.046, pad=0.04) | |
plt.errorbar(x, y, yerr) | |
plt.xlabel('x') | |
plt.ylabel('y') | |
plt.title('Quadratic with Poissonian error in y') | |
plt.legend(loc=0) -> determines optimal location | |
SECONDARY AXIS: | |
ax2 = ax2.twinx() | |
SUBPLOTS: | |
f, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3, sharey=True, figsize=(12,7)) | |
f.subplots_adjust(wspace=0, hspace=.3) | |
f.suptitle("HPGe April 2016", fontsize=14) | |
plt.setp([a.get_yticklabels() for a in f.axes[1:3&5:]], visible=False) | |
axes = [ax1, ax2, ax3, ax4, ax5, ax6] | |
plt.tightlayout() | |
TWO SUBPLOTS WITH COLORBAR: | |
fig, [ax1, ax2] = plt.subplots(1, 2, figsize=(12, 6), sharey=True) | |
plt.tight_layout() | |
... | |
norm_image = ImageNormalize(vmin=1e-4, vmax=5e-2, stretch=LogStretch()) | |
fitsplot = ax2.imshow(np.ma.masked_where(xdf_image.mask, xdf_image_clipped), norm=norm_image) | |
cbar_ax = fig.add_axes([1, 0.09, 0.03, 0.87]) | |
cbar = fig.colorbar(fitsplot, cbar_ax, ticks=LogLocator(subs=range(10))) | |
#labels = ['$10^{-4}$'] + [''] * 8 + ['$10^{-3}$'] + [''] * 8 + ['$10^{-2}$'] | |
#cbar.ax.set_yticklabels(labels) | |
cbar.set_label(r'Flux Count Rate ($e^{-1}/s$)', rotation=270, labelpad=30) | |
PLOTS: | |
plt.figure(figsize=(1,1)) = set size | |
plt.errorbar(x, y, yerr, fmt=‘.’) | |
FITS FILES: | |
plt.imshow(data, vmin=3, vmax=20, origin='lower’, clim=0,100) # Can fiddle with vmin and vmax | |
plt.colorbar() | |
LaTeX FORMATTING: | |
# import module that sets formatting parameters | |
from matplotlib import rc | |
# change default font for all plot text to LaTeX font; also change font size | |
rc('font', **{'family': 'serif', 'serif': ['Computer Modern Roman'], 'size': 14}) | |
# allow TeX commands to be used in text strings | |
rc('text', usetex=True) | |
RASTERIZING: | |
(so files aren’t huge? turns vectors into pixels…) | |
fig.set_rasterized(True) | |
OR | |
plt.plot(x, y, rasterized=True) # only partial rasterization | |
fig.savefig('rasterized.eps', dpi=144) | |
When you think you are ready…. multiprocessing: | |
https://docs.python.org/3.5/library/multiprocessing.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MACHINES: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
TO DETERMINE THE OS & VERSION | |
uname -a | |
TEXT EDITORS: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
nano = shows hints | |
vi = good for small edits | |
emacs = sucks always | |
FILES: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
TO COMPARE TWO FILES: | |
vimdiff file1 file2 | |
HOW TO SEARCH INSIDE FILES: | |
mdfind “string” -onlyin dir | |
OPEN .TAR.GAZ FILES: | |
gunzip -c foo.tar.gz | tar xopft - | |
curl -O address = download file at address | |
open -a “Application” file.txt = opens file.txt with specified Application | |
grep word file.txt = loops for the string ‘word’ in file.txt; add -i to make case-insensitive | |
wc = prints line/word/character count | |
cat = prints contents of a file in terminal | |
sort = outputs contents sorted alphabetically | |
uniq = outputs only unique lines | |
> file.txt = outputs to file.txt | |
>> file.txt = appends output to file.txt | |
sed 's/onething/somethingelse/g' file.txt = replaces 'onething' in file.txt with | |
'something else' everywhere in file.txt (remove g to do once per line) | |
head -n file.txt = prints first n lines of file.txt | |
tail -n file.txt = same, but last n lines | |
touch = create new file OR update timestamp to now | |
BASH STUFF: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
PATH stuff: | |
echo $PATH | tr ':' '\n' = shows list of paths (think of as list of books that bash knows where to access…?) | |
export PATH=‘usr/local/bin:$PATH’ = add a new location to path | |
REMOTE SERVERS: | |
ssh user@server = connect | |
scp user@server:address/file.txt destination = securely copies/pulls file on the server to the destination | |
After editing .bash_profile file: | |
source ~/.bash_profile = applies changes | |
? = wildcard, single character | |
history = view all things typed in ever | |
something | pbcopy = puts output of something in your clipboard | |
OR pbcopy < file.txt | |
rmdir OR rm -r = to remove a directory | |
cp -r = to copy a directory | |
ls -S = sort by size | |
ls -t = sort by time | |
ls -G = color code | |
find something = finding something | |
| = pipe! transfers the output of a command to another command | |
FOR PERMISSIONS: | |
chmod u=rwx,g=rwx,o= [filetext] | |
(user, group, other/world; r = read, w = write, x = execute) | |
CREATING EXECUTABLES: | |
#!/usr/bin/env python <— First line! Make sure to run the right kind of python | |
SCREEN: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
screen -S yoohoo = starts screen with name yoohoo | |
CTRL+A, d = detach from screen | |
CTRL+A, c = create new screen | |
CTRL+A, 1 = switches to the 1st screen | |
CTRL+A, CTRL+A = switches to last active screen | |
CTRL+A, A = allows you to type in title | |
screen -ls = list all screens | |
screen -r yoohoo = reattach to screen | |
exit = terminate screen | |
GITHUB: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
git clone ~address~ = copy a repository | |
git status = how is this dir different from the origin dir/what changes have been made? | |
git checkout ~branchname~ = start working in a branch called branchname | |
git checkout -b ~branchname~ = start working in a NEW branch called branchname | |
git branch -a = show all branches! | |
git add ~file already in the directory~ = add it to a list of committable files | |
git commit -m ‘comment’ = yes, I really want to commit all added files! | |
git rm file.txt = remove a file from git repo & directory | |
git rm —-cached file.txt = remove a file from git repo, keep in directory | |
git push ~A~ ~B~ = push changes from ~B~ to ~A~ | |
git push origin ~A~ = seal the deal? (have to pull first) | |
git pull = update directory to match the latest one | |
FOR ENVIRONMENTS: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
to install/remove packages: | |
conda install --name [name of environment] [name of package] | |
conda remove --name [name of environment] [name of package] | |
source activate astroconda = activate astroconda env | |
conda info —envs = list of envs | |
conda list = list of all packages in current environment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment