Shell
Scientific Computing
Why Shell?
What is the “shell”?
- For lack of a better word, the command line shell.
- We’ve called it the terminal.
- Thus far, we’ve used the shell to run
python
andnvim
- We’ve also briefly used a single shell command -
ls
- Recall we used it to see we’d written a file with
nvim
- Recall we used it to see we’d written a file with
Conceptualizing
- So far we’ve used the shell as follows:
On the Shell
- Shell scripting is used to control compute clusters
- These clusters do complex computations like climate modelling, protein folding.
- Sometimes called “HPC” - high performance computing.
- We can practice using the same techniques on our own computers!
More on the Shell
- Shell scripting is durable
- Unlike Google Drive or Dropbox, shell scripts from the 70s and 80s (more or less) work today
- Similar arguments for Neovim vs. Microsoft Office Word or Google Docs
- Shell scripting is portable
- People all around the world use the exact same commands.
Automation
- Shell scripting can be automated
- Sometimes we just want a computer to do something without us thinking about it.
- Say you want to collate data every Sunday at 2 PM PT.
- Write a
.py
script that collates - Schedule it to run automatically with the shell
cron
command
- Write a
Why not the shell?
- Non-graphical - When first learning, students are used to see pictures and clicking them
- OS-based - Windows and MacOS have different shell scripts
- Steep-learning curve - Like Neovim, students generally find it difficult to get started, though it is widely preferred by experienced users
Git Bash
Setup
- We need to do a bit more setup to:
- Make the course run smoothly
- Learn the general, transferrable skills we want.
Bash
- The standard for shell scripting is “bash”
- (It’s free)
Windows
MacOS
- MacOS uses
zsh
- It is close enough to bash to use in this course.
- You will still need to install
git
, however, a critical shell command.- Install Git for macOS
- It installs through command line!
Why standardize?
- It is bad enough to have to say “type
python
orpython3
” - But even just using
ls
we see perhaps frustrating differences.ls
on bash lists only file namesls
on PowerShell lists file names, dates, and morels
oncmd.exe
is an error.
Diving In
- Today we will use shell scripting in the terminal to…
- Delete old files
- Organize files within folders
- Save the output of scripts as files
Starting Point
- Let’s see what
.py
files you have. - We’ll issue an
ls
command. - We’ll provide an argument to the command.
- We’ll use the
.py
file extension in the command - And importantly, we’ll use the “wildcard”
*
Wildcard
- The
*
will “match” any name. - So
.py
will “match” and Python file! - I don’t know what you’ve saved, I see this!
$ ls *.py
args.py hi.py pw.py reply.py tax.py
- What about
ls *
?ls *w.py
?
The File System
File Management
- I tend to like to:
- Delete old files
- Organize files within folders
- Save the output of scripts as files
- Let’s do that.
rm
Warning
This command is dangerous.
- The
rm
command removes files by name. - For example, I don’t use
reply.py
anymore. - I can remove it like so:
$ ls *.py
args.py hi.py pw.py reply.py tax.py
$ rm reply.py
$ ls *.py
args.py hi.py pw.py tax.py
Remove/Recycle
- You may be used to being able to restore deleted files from a recycling bin on your device.
- Restoring from
rm
is sometimes possible, but never easy. - Be conscientious!
- Learning to think about file deletion is an important part of the scientific computing process!
Alternative
- Sometimes I don’t want to look at a file, but I don’t want to delete it either.
- I often keep a folder around called “old”
- It’s stuff that’s old!
- I can make a folder with
mkdir
and the folder name, so
$ ls
args.py hi.py pw.py tax.py
$ mkdir old
$ ls
args.py hi.py old pw.py tax.py
Movement
Moving Files
- You may be used to “drag and drop” movement of files between folders.
- In bash, we can do this using the
mv
command. - It takes two arguments.
- The file to move, and
- A folder to move it to.
$ ls
args.py hi.py old pw.py tax.py
$ mv hi.py old
args.py old pw.py tax.py
- Where is
hi.py
?
Moving in Terminal
- When we are using the terminal, we have a notion of some location
- It is the same kind of location a file can have, that is, within some folder.
- These locations are called “paths” and are defined by how we get to them.
Locating ourselves
- You can see where you are at in the terminal at any time with
pwd
- Stands for “print working directory”
- If you just opened Terminal on Windows you would see:
$ pwd
/c/Users/cd-desk
- On MacOS, I believe
$ pwd
/home/user
Moving ourselves
- To change which folder (or directory) the terminal is currently working within
- The “working directory”
- We use the “change directory” command
cd
$ ls
args.py old pw.py tax.py
$ pwd
/home/user
$ cd old
$ ls
hi.py
$ pwd
/home/user/old
Movement
- We can move a file, like
hi.py
, to a folder, likeold
$ mv hi.py old
- We can move the terminal’s current location to folder, like
old
$ cd old
Exiting a Folder
- To leave a folder after
cd
ing into it, we use a special destination
$ cd ..
user@DESKTOP-THMS2PJ:~$ pwd
/home/user
..
is the name of the folder that contains the folder you are currently in.- So you can use
cd
to change to the containing folder, often called the “parent”
Graphically
Removing Directories
- Like files, which we can remove with
rm
, we can also remove folders (or directories). - We do so with
rmdir
- Let’s try on
old
$ rmdir old
rmdir: failed to remove 'old': Directory not empty
- You can only remove folders this way if they are empty.
- This helps you not delete things by accident!
Files
Reading
- The names of files are helpful, but not always all we want to know.
- When files only contain text - like
.py
files, we can usecat
to see what text they contain.
$ cat args.py
import sys
print(sys.argv[0])
Writing
- Really, the best way to write a file from the command line is with Neovim.
- But there are other ways, and sometimes you just want to make a file to test something.
- You can make an empty file (with noting in it) via
touch
$ touch somefile.txt
$ ls *.txt
somefile.txt
Echoing
- To put anything in a file, we often use an
echo
command. echo
is also used for shell “Hello, world!”
$ echo "Hello, shell!"
Hello, shell!
Redirecting
- For any shell command - including
echo
- we can “redirect” what is printed to some file. - We do so using the special shell
>
operator- It’s just a special thing, like
*
- It’s just a special thing, like
$ echo "Hello, redirect!" > hi.txt
$ cat hi.txt
Hello, redirect!
- This is very helpful to save the results of Python computations.
Example
- Here’s an example how it should work!
$ python3 tax.py 40000 > tax_burden.txt
$ cat tax_burden.txt 115529.25
References
- You can see the commands used here in a table reference in an Appendix
- I’ll include them here as well!
Command | Action |
---|---|
ls |
list files in folder |
rm f.txt |
delete file “f.txt” |
mkdir dir |
create a new folder named “dir” |
mv f.txt dir |
move file “f.txt” to folder “dir” |
pwd |
print current folder |
cd dir |
change current folder to “dir” |
cd .. |
go to “parent” folder |
echo hi |
print the text “hi” |
echo hi >f.txt |
write “hi” to file “f.txt” |
cat f.txt |
see the text in f.txt |
Vim Motions
Command Mode
- The beauty of Neovim is how powerful command mode, vim motions, and vim operators can be.
- We’ll just introduce a few.
- The goal is to give a sense of what is possible, and give you things to start practicing.
- If you ever want to do something in Neovim, chances are you can!
Solution
Insert is easy
- After opening the file, just press
i
and start typing…
$
is end-of-line
- ESC to exit
i
mode - “hjkl” are left-up-down-right
- So
j
down to the 415050 line
- So
$
jumps to the end of the line
0
is start-of-line
- We’ll now copy that comment rather than re-typing the whole thing
- ESC into command mode
0
brings to beginning of line./
let’s us search, so/#
searches for the start of the comment- Windows I had to “ENTER” after.
Practice this a few times!
Aside: Mouse
Tip
I know you can do this with the mouse.
- I type a lot.
- I promise this is faster once you practice.
- Took me around 2 weeks.
- Like 15% of a semester.
- It is way faster than laptop trackpads.
Yank & Paste
- Vim predates copy paste!
- Type
y
to yank and then$
to yank until the end of the line.- We can combine motions (
$
) and operators (y
)
- We can combine motions (
- Use
j
to go down to the next line. - Use
p
to paste the comment.
Update it
- I used
/9
- first character to changer
- replace mode- Type “5”.
l
to move right (onto “6”) thendl
to delete the next character.
Add the range
- I used
/over
- first character to changede
- We introduce the newe
motion for “end of word” and combine with delete.- Type “between 413350 and”
- I escaped (ESC) and saved (
:w
)
Scientific Writing
Writing as Thinking
- I consider writing part of the scientific process.
- You may realize while working through this that…
- Each tax braket is a simple linear relation!
- The rate is the slope
- The intercept is how much they don’t have to pay due to marginal rates.
- By writing and explain our computations, we can uncover insights
Naive notes
- Here’s some naive notes I took.
- They can be made better!
Compute Intercept
- We know e.g. the tax cost at 500000
- Compare to a non-marginal 39.6%
- We can find the difference.
A Linear Equation
- Try 450000
- Try 650000
- We can precompute these and make much simpler functions
Aside: Slope-Intercept
- I think of this as \[ y = mx + b \]
- The slope-intercept form of a line, or linear equation.
- We calculated it from the point-slope form of a line.
Aside: Scientific Application
- Thinking in slopes and intercepts is a powerful scientific skill!
- Recognize this?
Exercise
Intercept Computation
- One of my favorite ways to use Python, Neovim, and the shell is to:
- Compute some numerical values - Intercept values for linear equations of tax.
- Run the script and output the results to the shell.
- Use
>
to direct the output to file. - Use
nvim
to edit that file into a new Python script that uses the numerical values I found.
First
- Write a python script,
points.py
, that computes the tax cost at the end of each bracket. - Here it is for “Married Filing Jointly”
- You can do this with a loop using the
tax_policy
list-of-lists!
Aside: Loop Version
Second
- Run the Python script and direct the output to some file, like
tax.py
$ python points.py > tax.py
$ nvim tax.py
- From here, you can convert to a Python list of your choosing with a variety of means.
Aside: Visual Block
Warning
This is an advanced topic.
- We introduce visual block mode
- A way to modifying multiple lines all at once.
- There is also a regular “visual mode”
v
which I use less often.
- There is also a regular “visual mode”
Example
nvim tax.py
- ctrl+q or ctrl+v to enter “visual block”
5j
to move down 5 linesI
(shift + “i”) to enter “insert block”- Type “[” to begin a list
- ESC to exit “insert block”
- “[” will appear at the start of every line.
Aside: Printing Python
- Alternatively, you can use
print()
to directly print things usable as Python.
Third
- Use points to determine intercepts
- Consider:
- We can calculate it as follows:
Fourth
- Given the calculated slopes and intercepts, construct a piecewise function capturing income tax.
- Something like this:
Exercise
- Create two Python files:
- One which calculates intercepts - Perhaps
intercepts.py
- One which uses intercepts to compute tax cost linearly. - Perhaps
tax_line.py
- One which calculates intercepts - Perhaps
Solution pt. 1
Solution pt. 2
Code
# Generate with `intercepts` then modify with visual block mode
taxes = [
[9275, 0.1, 0.0],
[37650, 0.15, -463.75],
[91150, 0.25, -4228.75],
[190150, 0.28, -6963.25],
[413350, 0.33, -16470.75],
[415051, 0.35, -24737.75],
]
def single_tax(income):
# Check all brackets
for tax in taxes:
if income < tax[0]:
return income * tax[1] + tax[2]
# We calculated the top bracket earlier
return income * .396 + -43830.05
Comments
Taking notes
cat
it, I am unlikely to knowDocumentation
#
Update
pw.py
pw.py
:Updating
tax.py