GA 1.3: Warmup Notebook¶
CEGM1000 MUDE: Week 1.3. September 20, 2024.
This notebook is designed to help you prepare for GA 1.3, as we do not release the entire assignment in advance. This is formatted similarly to a PA and is meant for you to read, practice and explore - we hope you find it useful!
Note: this is the first time we have tried a "warmup" notebook in MUDE, so the format and scope of this activity may change throughout the semester.
import numpy as np
from scipy import interpolate
import pandas as pd
import matplotlib.pyplot as plt
np.set_printoptions(precision=3)
Part 1: Dictionary Review¶
Several functions in GA 1.3 require the use of a Python dictionary to make it easier to keep track of important data, variables and results for the various models we will be constructing and validating.
It may be useful to revisit PA 1.1, where there was a brief infroduction to dictionaires. That PA contains all the dictionary info you need for GA 1.3. A read-only copy is here and the source code (notebook) is here.
$\textbf{Task 1.1}$
Read and run the cell below to make sure you remember how to use a dictionary.
Modify the function to print some of the other key-value pairs of the dictionary.
my_dictionary = {'key1': 'value1',
'key2': 'value2',
'name': 'Dictionary Example',
'a_list': [1, 2, 3],
'an_array': np.array([1, 2, 3]),
'a_string': 'hello'
}
def function_that_uses_my_dictionary(d):
print(d['key1'])
# ADD MORE CODE HERE
if 'new_key' in d:
print('new_key exists and has value:', d['new_key'])
return
function_that_uses_my_dictionary(my_dictionary)
$\textbf{Task 1.2}$
Test your knowledge by adding a new key new_key
and then executing the function to print the value.
YOUR_CODE_HERE
function_that_uses_my_dictionary(my_dictionary)
Hint
Once you make the modifications, you should be able to reproduce the following output (depending on your value for new_key
):
value1
Dictionary Example
[1, 2, 3]
[1 2 3]
hello
new_key exists and has value: new_value
$\textbf{Task 1.3}$
Run the cell below to print all of the keys and the value type
. Then modify the code to print the values directly.
print("Keys and Values (type):")
for key, value in my_dictionary.items():
print(f"{key:16s} --> {type(value)}")
Part 2: Importing Functions from a *.py
file¶
Sometimes it is useful to put code in *.py
files. It is very easy to import the contents of these files into a notebook. The code cell below imports the entire contents of file warmup.py
into the notebook.
$\textbf{Task 2}$
Open the file and read the contents. Then run the cell below to use the function that is inside it to confirm that it is imported properly.
from warmup import *
YOUR_CODE_HERE
Part 3: Data Import and Interpolation¶
This Part illustrates the process of importing datasets, doing a bit of manipulation to both the observations and the times, then doing a bit of interpolation. The code will run without error as-is, so first you should read each step and try to understand the functions/methods used. Then, modify the code to explore each object; in particular, use the print
, type
and shape
methods.
dataset1 = pd.read_csv('./data_warmup/dataset1.csv')
times1 = pd.to_datetime(dataset1['times'])
obs1 = (dataset1['observations[m]']).to_numpy()*1000
dataset2 = pd.read_csv('./data_warmup/dataset2.csv')
times2 = pd.to_datetime(dataset2['times'])
obs2 = (dataset2['observations[mm]']).to_numpy()
print(type(dataset1), '\n',
type(dataset2), '\n',
type(times1), '\n',
type(times2), '\n',
type(obs1), '\n',
type(obs2))
print(np.shape(dataset1), '\n',
np.shape(dataset2), '\n',
np.shape(times1), '\n',
np.shape(times2), '\n',
np.shape(obs1), '\n',
np.shape(obs2))
# DO MORE STUFF TO EXPLORE WHAT IS IN THE DATA!
You may have noticed that the two datasets are not made at identical time increments. This is a problem if we want to compare them, or use them together in an analysis where values of the observations are needed at identical observation times. You will therefore have to interpolate the data to the same times for a further analysis. You can use the SciPy function interpolate.interp1d
(read its documentation).
The cells below do the following:
- Define a function to convert the time unit
- Convert the time stamps for all data
- Use
interp1d
to interpolate the measurements ofdataset2
at the time of the measurements ofdataset1
def to_days_years(times):
'''Convert the observation times to days and years.'''
times_datetime = pd.to_datetime(times)
time_diff = (times_datetime - times_datetime[0])
days_diff = (time_diff / np.timedelta64(1,'D')).astype(int)
days = days_diff.to_numpy()
years = days/365
return days, years
days1, years1 = to_days_years(times1)
days2, years2 = to_days_years(times2)
interp = interpolate.interp1d(days2, obs2)
obs2_at_times_for_dataset1 = interp(days1)
print(type(obs2_at_times_for_dataset1), '\n',
np.shape(obs2_at_times_for_dataset1), '\n',
obs2_at_times_for_dataset1)