import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt

'''
count_times = the time since the start of data-taking when the data was 
               taken (in seconds)
count_rates = the number of counts since the last time data was taken, at 
               the time in count_times
'''

count_times = np.loadtxt("count_rates.txt", dtype=int)[0]
count_rates = np.loadtxt("count_rates.txt", dtype=int)[1]

# Put your code here - add additional cells if necessary



"""
Explanation: Homework #2
This notebook is due on Friday, October 7th, 2016 at 11:59 p.m..  Please make sure to get started early, and come by the instructors' office hours if you have any questions.  Office hours and locations can be found in the course syllabus.  IMPORTANT: While it's fine if you talk to other people in class about this homework - and in fact we encourage it! - you are responsible for creating the  solutions for this homework on your own, and each student must submit their own homework assignment.
Some links that you may find helpful:

Markdown tutorial
The Pandas website
The Pandas tutorial
10-minute Panda Tutorial
All CMSE 201 YouTube videos

Your name
Put your name here!

Section 1: Radioactivity wrapup
In this part of the homework, we're going to finish what we started regarding modeling the count rate of radioactive data that you worked wtih in class, to try to estimate the strength of the radioactive background that was seen in the radioactive count rates.
In class, we discussed that for radioactive material with an initial amount $N_0$ and a half life $t_{1/2}$, the amount left after time t is $N(t) = N_0 2^{-t/t_{1/2}}$.  The expected radioactive decay rate is then:
$\mathrm{CR}(t) = - \frac{dN}{dt} = \frac{N_0 \ln 2}{t_{1/2}}2^{-t/t_{1/2}}$
However, the data doesn't agree well with this - there's something contaminating our count rate data that's causing a radioactive "background" that is approximately constant with time.  A better estimate of the count rates is more like:
$\mathrm{CR}(t) = \mathrm{CR}{\mathrm{S}}(t) + \mathrm{CR}{\mathrm{BG}}$
where $\mathrm{CR}{\mathrm{S}}(t)$ is the count rate from the sample, which has the shape expected above, and $\mathrm{CR}{\mathrm{BG}}$ is the count rate from the radioactive background.
We're now going to try to figure out the values that go into the expressions for $\mathrm{CR}{\mathrm{S}}(t)$ and $\mathrm{CR}{\mathrm{BG}}$ by using the data.  What you're going to do is:

"Smooth" the decay rate data over N adjacent samples in time to get rid of some of the noise.  Try writing a piece of code to loop over the array of data and average the sample you're interested in along with the N samples on either side (i.e., from element i-N to i+N, for an arbitrary number of cells).  Store this smoothed data in a new array (perhaps using np.zeros_like() to create the new array?).
Plot your smoothed data on top of the noisy data to ensure that it agrees.
Create a new array with the analytic equation from above that describes for the decay rate as a function of time, taking into account what you're seeing in point (2), and try to find the values of the various constants in the equation.  Plot the new array on top of the raw data and smoothed values. 

Note that code to load the file count_rates.txt has been added below, and puts the data into two numpy arrays as it did in the in-class assignment.
End of explanation
"""


import pandas

erie = pandas.read_csv('erie1918Ann.csv', skiprows=2)
miHuron = pandas.read_csv('miHuron1918Ann.csv', skiprows=2)
ontario = pandas.read_csv('ontario1918Ann.csv', skiprows=2)
superior = pandas.read_csv('superior1918Ann.csv', skiprows=2)

"""
Explanation: Question:  What are the constants that you came up with for the count rate equation?  Do these values make sense given the experimental data?  Why or why not?
put your answer here!

Section 2:  Great Lakes water levels
The water level in the Great Lakes fluctuates over the course of a year, and also fluctuates in many-year cycles.  About two and a half years ago (in Feb. 2014), there was an article in Scientific American describing the historically low levels of the Great Lakes - in particular, that of Lake Michigan and Lake Huron, which together make up the largest body of fresh water in the world.  In this part of the homework assignment, we're going to look at water height data from the Great Lakes Environmental Research Laboratory - in particular, data from 1918 to the present day.  In the cell below this, we're using Pandas to load four CSV ("Comma-Separated Value") files with data from Lake Eric, Lakes Michigan and Huron combined, Lake Ontario, and Lake Superior into data frames.  Each dataset contains the annual average water level for every year from 1918 to the present.  Use these datasets to answer the questions posed below.
End of explanation
"""


# Put your code here!



"""
Explanation: Question 1:  Calculate the mean water levels of all of the Great Lakes over the past century (treating Lakes Michigan and Huron as a single body of water).  Are all of the values similar?  Why does your answer make sense?  (Hint: where is Niagara Falls, and what direction does the water flow?)
put your answer here!
End of explanation
"""


# Put your code here



"""
Explanation: Question 2: Make a plot where you show the fluctuations of each lake around the mean value from the last century (i.e., subtracting the mean value of the lake's water level from the data of water level over time).  In general, do you see similar patterns of fluctuations in all of the lakes?  What might this suggest to you about the source of the fluctuations?  
Hint: you may want to use pyplot instead of the built-in Pandas plotting functions!
put your answer here!
End of explanation
"""


# Put your code here



"""
Explanation: Question 3: Finally, let's look at the original issue - the water level of the Lake Michigan+Lake Huron system and how it changes over time.  When you examine just the Lake Michigan data, zooming in on only the last 20 years of data, does the decrease in water level continue, does it reverse itself, or does it stay the same?  In other words, was the low level reported in 2014 something we should continue to be worried about, or was it a fluke?
put your answer here!
End of explanation
"""


#  put your code and plots here!



"""
Explanation: Section 3: Modeling drug doses in the human body
Modeling the behavior of drugs in the human body is very important in medicine.  One frequently-used model is called the "Single-Compartment Drug Model", which takes the complex human body and treats it as one homogeneous unit, where drug distribution is instantaneous, the concentration of the drug in the blood (i.e., the amount of drug per volume of blood) is proportional to the drug dosage, and the rate of elimination of the drug is proportional to the amount of drug in the system.  Using this model allows the prediction of the range of therapeutic doses where the drug will be effective.
We'll first model the concentration in the body of aspirin, which is commonly used to treat headaches and reduce fever.  For adults, it is typical to take 1 or 2 325 mg tablets every four hours, up to a maximum of 12 tablets/day.  This dose is assumed to be dissolved immediately into the blood plasma.  (An average adult human has about 3 liters of blood plasma.)  The concentration of drugs in the blood (represented with the symbol Q) is typically measured in $\mu$g/ml, where 1000 $\mu$g (micrograms) = 1 mg (milligram).  For aspirin, the does that is effective for relieving headaches is typically between 150-300 $\mu$g/ml, and the half-life for removal of the drug from the system is about 3.2 hours (more on that later).
The rate of removal of aspirin from the body (elimination) is proportional to the amount present in the system:
$\frac{dQ}{dt} = -K Q$
Where Q is the concentration, and K is a constant of proportionality that is related to the half-life of removal of drug from the system:  $K = 0.693 / t_{1/2}$.
Part 1: We're now going to make a simple model of the amount of aspirin in the human body.  Assuming that an adult human has a headache and takes 2 325 mg aspirin tablets.  If the drug immediately enters their system, for how long will their headache be relieved?  Show a plot, with an appropriate title, x-axis label, and y-axis label, that shows the concentration of aspirin in the patient's blood over a 12-hour time span.  In your model, make sure to resolve the time evolution well - make sure that your individual time steps are only a few minutes!
Put your answer immediately below, and the code you wrote (and plots you generated) to solve this immediately below that.
put your answer here!
End of explanation
"""


#  put your code and plots here!



"""
Explanation: Part 2: We're now going to model the concentration of a drug that needs to be repeatedly administered - the drug Dilantin, which is used to treat epilepsy.  The effective concentration of the drug in humans is 10-20 $\mu$g/ml, the half-life of Dilantin is approximately 22 hours, and the drug comes in 100 mg tablets which are effectively instantaneously released into your bloodstream.  For this particular drug, only about 12% of the drug in each dose is actually available for absorption in the bloodstream, meaning that the effective amount added to the blood is 12 mg per dose.
Assuming that the drug is administered every 8 hours to a patient that starts out having none of the drug in their body, make a plot of the drug concentration over a ten day period and use it to answer the following two questions:

How long does it take to reach an effective concentration of the drug in the patient's blood?
By roughly when does the drug concentration reach a steady state in the patient's blood?  (In other words, after how long is the concentration neither rising nor falling on average?)

put your answer here!
End of explanation
"""


from IPython.display import HTML
HTML(
"""
<iframe
    src="https://goo.gl/forms/Px7wk9DcldfyCqMt2?embedded=true" 
    width="80%" 
    height="1200px" 
    frameborder="0" 
    marginheight="0" 
    marginwidth="0">
    Loading...
</iframe>
"""
)

"""
Explanation: Section 4: Feedback (required!)
End of explanation
"""


