Homework 3

Due Date: Tuesday, September 20th at 11:59 PM


Problem 1: Git and recovering from a mistake

You will do this problem in the Jupyter notebook so I can see your output. Once again, you will work with your playground repository.

NOTE: At the beginning of each cell, you MUST type %%bash. If you don't do that then you will not be able to work with the necessary bash commands.

Follow the following steps for this problem:

  1. First cell:
    1. Type cd /tmp to enter the temporary directory
    2. git clone url_to_your_playground_repo
  2. Second cell:
    1. Go into your local playground directory (cd /tmp/playground)
    2. Type git pull origin mybranch1
    3. ls
  3. Third cell:
    1. Go into your local playground directory (cd /tmp/playground)
    2. Type git status
  4. Fourth cell:
    1. Go into your local playground directory (cd /tmp/playground)
    2. Type git reset --hard origin/master
    3. ls
  5. Fifth cell:
    1. Go into your local playground directory (cd /tmp/playground)
    2. Type git status

The whole point of this problem was to show you how to get your local repo back to an earlier state. In this exercise, you accidentally merged something to master that you didn't want. Rather than starting to delete things all over the place, you can simply reset your HEAD to a previous commit.


In [15]:
%%bash
cd /tmp
rm -rf playground
git clone https://github.com/crystalzhaizhai/playground.git


Cloning into 'playground'...

In [16]:
%%bash
cd /tmp/playground
git pull origin mybranch1
ls


Merge made by the 'recursive' strategy.
 book.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 book.md
book.md
feature.txt
intro.md
world.md
From https://github.com/crystalzhaizhai/playground
 * branch            mybranch1  -> FETCH_HEAD

In [20]:
%%bash
cd /tmp/playground
git status


On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

In [21]:
%%bash
cd /tmp/playground
git reset --hard origin/master
ls


HEAD is now at 5b6fdc2 Shared attribution between Joe and Sally
feature.txt
intro.md
world.md

In [22]:
%%bash
cd /tmp/playground
git status


On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean

Problem 2: Git and checking out a single file

Sometimes you don't want to merge an entire branch from the upstream but just one file from it. There is a direct use case for such a situation. Suppose I've made an error in this homework (or a lecture) and want to correct it. I fix the mistake in the upstream repo. In the meantime you have edited some other files and you really don't want to manually ignore my older copies of those files. Rather, you want to fix just one file from this new branch. This is how you do it.

As usual, be sure to type in %%bash before you write any bash commands in a cell.

Note: The steps below assume that you have already cloned the playground repo in this notebook.

  1. First cell:
    1. Go into the playground repo and fetch the changes from the master branch of the course remote.
  2. Second cell:
    1. git checkout course/master -- README.md. The -- means that README.md is a file (as opposed to a branch).
    2. cat README.md. This just looks at the updated file.
  3. Third cell:
    1. git status
    2. Commit the changes to your local repo with an appropriate commit message.
    3. git status
    4. Push the changes to your remote repo.

In [27]:
%%bash
cd /tmp/playground
cat .git/config


[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://github.com/crystalzhaizhai/playground.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

In [31]:
%%bash
cd /tmp/playground
git remote add course https://github.com/IACS-CS-207/playground.git
cat .git/config


[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://github.com/crystalzhaizhai/playground.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[remote "course"]
	url = https://github.com/IACS-CS-207/playground.git
	fetch = +refs/heads/*:refs/remotes/course/*
fatal: remote course already exists.

In [32]:
%%bash
cd /tmp/playground
git fetch course master


From https://github.com/IACS-CS-207/playground
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> course/master

In [35]:
%%bash
cd /tmp/playground
git checkout course/master -- README.md
cat README.md


# Playground Repo

For practicing.

In [90]:
%%bash
cd /tmp/playground
git add .
git commit -m "playgroundchange" -a
git status
git push


On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean
Everything up-to-date

Problem 3

This problem is related to the Lecture 4 exercises.

  1. Open the languages.txt file. This file contains all the languages that students listed as their primary language in the course survey.
  2. Load the language strings from the file into a list.
  3. Use the Counter method from the collections library to count the number of occurrences of each element of the list.
    • NOTE: It is not necessary to use the most_common() method here.
  4. Create a bar plot to display the frequency of each language. Be sure to label the x-axis!
    • Remember, to create plots in the notebook you must put the line %matplotlib inline at the beginning of your notebook.
    • Be sure to import matplotlib: import matplotlib.pyplot as plt.
    • To generate the bar plot write plt.bar(x_coords, freqs). You need to define x_coords and freqs.
    • Hint: You may want to use the numpy arange function to create x_coords. Remember, x_coords is the x-axis and it should have points for each distinct language.
    • Hint: To get freqs, you may want to use the values() method on your result from step 3. That is, freqs = result_from_3.values().
    • Hint: To label the x-axis you should use plt.xticks(x_coords, labels) where labels can be accessed through the keys() method on your result from step 3.

In [50]:
with open("../../lectures/L4/languages.txt","r") as f:
    primary_course=f.read().split()
from collections import Counter
course_count=Counter(primary_course)

In [57]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x_coords=np.arange(len(course_count))
total=np.sum(course_count.values())
freqs=course_count.values()
plt.xticks(x_coords,course_count.keys())
plt.bar(x_coords,freqs)


Out[57]:
<Container object of 8 artists>

Problem 4

In chemical kinetics, the reaction rate coefficient for a given reaction depends on the temperature of the system. The functional relationship between the reaction rate coefficient and temperature is given by the Arrhenius rate: \begin{align} k\left(T\right) = A T^{b}\exp\left(-\frac{E}{RT}\right) \end{align} where $A$, $b$, and $E$ are parameters, $R = 8.314 \dfrac{\textrm{J}}{\textrm{mol} \textrm{ K}}$ is the universal gas constant, and $T$ is the temperature.

Write a function which returns $k\left(T\right)$ given $A$, $b$, $E$, and $T$. Here are a few requirements:

  • The function should test for exceptions where necessary.
  • Pass the parameters $A$, $b$, and $E$ in as a list.
  • Make $R$ a keyword argument to the function.

In [3]:
def kinetics(p,T, R=8.314):
    import numpy as np
    if len(p)<3:
        print("Error! Less than 3 parameters")
        return()
    try:
        k=p[0]*(T**p[1])*np.exp(-p[2]/(R*T))
        return k
    except ZeroDivisionError:
        print("Error! Divided by 0")
        return()

In [4]:
kinetics([1,2],0)


Error! Less than 3 parameters
Out[4]:
()

Problem 5

Using numpy arrays, plot $k\left(T\right)$ for $T\in\left(0, 5000\right]$ for three different sets of parameters $\left\{A, b, E\right\}$. Make sure all three lines are on the same figure and be sure to label each line. You may use the function from Problem 2. You may want to play with the parameters a little bit to get some nice curves but you won't lose points for ugly curves either (as long as they're correct!).


In [113]:
TT=np.arange(1,5000)
plt.plot(TT,kinetics([3,6,2],TT),'r')
plt.plot(TT,kinetics([4,5,6],TT),'g')
plt.plot(TT,kinetics([6,5,4],TT),'b')
plt.legend(["A,b,E=[3,6,2]","A,b,E=[4,5,6]","A,b,E=[6,5,4]"])
plt.xlabel("T")
plt.ylabel("k")
plt.title("kinetics")


Out[113]:
<matplotlib.text.Text at 0x11b564518>

In [110]:
%%bash
git add "HW3_final.ipynb"

git commit -m "HW3" -a
git status
git remote
git push origin master


[master b7fbb64] HW3
 1 file changed, 7 insertions(+), 7 deletions(-)
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
course
origin
upstream
To https://github.com/crystalzhaizhai/cs207_yi_zhai.git
   cffa62f..b7fbb64  master -> master

In [ ]: