<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Numpy
Step1:  If we want to multiply each number in that list by 3, we can certainly do it by looping through and multiplying each individual number by 3, but that seems like way too much work. Let's see what Numpy can do instead.
First, we need to import the Numpy package, which we commonly rename as "np."
Step2:  Here, we created a numpy array from scratch. You can also convert a list or tuple into an array
Step3:  There are a couple of ways to check if something is an array (as opposed to a list), but here's a really straight-forward way
Step4:  Let's say you want to know what the first element in the array is. You can select elements of arrays the same way you do for lists
Step5:  You can perform slices in the same way as well
Step6:  All the elements of an array must be the same type. For instance, you can't have both integers and floats in a single array. If you already have an array of integers, and you try to put a float in there, Numpy will automatically convert it
Step7:  Array arithmetic is always done element-wise. That is, the arithmetic is performed on each individual element, one at a time.
Step8:  What if you want an array, but you don't know what you want to put into it yet? If you know what size it needs to be, you can create the whole thing at once and make every element a 1 or a 0
Step9:  Notice that you can specify whether you want the numbers to be floats (the default) or integers. You can do complex numbers too.
If you don't know what size your array will be, you can create an empty one and append elements to it
Step10:  Extra
Step11:  There are some advantages of using np.arange() over range(); one of the most important ones is that np.arange() can take floats, not just ints.
Step12:  Numpy has some functions that make statistical calculations easy
Step13:  We can use Numpy to select individual elements with certain properties. There are two ways to do this
Step14:  Numpy is a great way to read in data from ASCII files, like the data files you got from the 20 m robotic telescope. Let's start by creating a data file and then reading it in, both using Numpy.
Step15:  Save the array to a file
Step16:  Now we can use Numpy to read the file into an array
Step17:  Matplotlib
Step18:  Matplotlib can create all the plots you need. For instance, we can plot our readThisData array from the Numpy session, which was two columns with 5 numbers each. Let's plot those numbers. It only takes a single line of code
Step19:  If we want lines to connect the points
Step20:  Here's another example of what Numpy and Matplotlib can do
Step21:  Let's add some labels and titles to our plot
Step22:  Let's change how this looks a little
Step23:  Here's another kind of plot we can do
Step24:  What if we want logarithmic axes? The easiest way is to use semilogx(), semilogy(), or loglog()
Step25:  When you're in the IPython Notebook, plots show up as soon as you make them. If, on the other hand, you're writing a separate script and running from the command line (by typing in "python script.py" where script.py is the name of the script you just wrote) OR typing everything into the command line, you'll need to explicitly tell python to plot it. The same three lines above would instead be
<ASSISTANT_TASK:>
Python Code:
a = [1,2,3]
b = 3*a
print b
Explanation: Numpy: "number" + "python"
Numpy is a Python package that is commonly used by scientists. You can already do some math in Python by itself, but Numpy makes things even easier.
We've already seen that Python has data structures such as lists, tuples, and dictionaries; <b>Numpy has arrays</b>. Arrays are just matrices, which you might have seen in math classes. With Numpy, we can do math on entire matrices at once. To start with, here's what happens if we make a list of numbers and try to multiply each number in that list by 3:
End of explanation
import numpy as np
a = np.array([1,2,3])
b = 3*a
print b
Explanation: If we want to multiply each number in that list by 3, we can certainly do it by looping through and multiplying each individual number by 3, but that seems like way too much work. Let's see what Numpy can do instead.
First, we need to import the Numpy package, which we commonly rename as "np."
End of explanation
c = [2,5,8]
print c
c = np.array(c)
print c
Explanation: Here, we created a numpy array from scratch. You can also convert a list or tuple into an array:
End of explanation
c
Explanation: There are a couple of ways to check if something is an array (as opposed to a list), but here's a really straight-forward way:
End of explanation
c[0]
c[1]
c[-2]
Explanation: Let's say you want to know what the first element in the array is. You can select elements of arrays the same way you do for lists:
End of explanation
c[0:2]
Explanation: You can perform slices in the same way as well:
End of explanation
d = np.array([0.0,1,2,3,4],dtype=np.float32)
print d,type(d)
d.dtype
d[0] = 35.21
print d
Explanation: All the elements of an array must be the same type. For instance, you can't have both integers and floats in a single array. If you already have an array of integers, and you try to put a float in there, Numpy will automatically convert it:
End of explanation
array1 = np.array((10, 20, 30))
array2 = np.array((1, 5, 10))
print array1 + array2
print array1 - array2
print 3*array1
print array1 * array2
Explanation: Array arithmetic is always done element-wise. That is, the arithmetic is performed on each individual element, one at a time.
End of explanation
ones_array = np.ones(5)
print ones_array
print 5*ones_array
zeros_array = np.zeros(5, int)
print zeros_array
Explanation: What if you want an array, but you don't know what you want to put into it yet? If you know what size it needs to be, you can create the whole thing at once and make every element a 1 or a 0:
End of explanation
a=[1,2,3]
a.append(4)
a
f = np.array(())
print f
f = np.append(f, 3)
f = np.append(f, 5)
# f.append(5)
print f
# Question: what if you want that 3 to be an integer?
g = np.append(f, (2,1,0))
print g
Explanation: Notice that you can specify whether you want the numbers to be floats (the default) or integers. You can do complex numbers too.
If you don't know what size your array will be, you can create an empty one and append elements to it:
End of explanation
print np.arange(10)
print np.arange(10.0)
print np.arange(1, 10)
print np.arange(1, 10, 2)
print np.arange(10, 1, -1)
Explanation: Extra: Figure out how to insert or delete elements
If you want an array of numbers in chronological order, Numpy has a very handy function called "arange" that we saw on the first day.
End of explanation
print range(5, 10, 0.1)
print np.arange(5, 10, 0.1)
Explanation: There are some advantages of using np.arange() over range(); one of the most important ones is that np.arange() can take floats, not just ints.
End of explanation
redshifts =  np.array((0.2, 1.56, 6.3, 0.003, 0.9, 4.54, 1.1))
print redshifts.min(), redshifts.max()
print redshifts.sum(), redshifts.prod()
print redshifts.mean(), redshifts.std()
print redshifts.argmax()
print redshifts[redshifts.argmax()]
Explanation: Numpy has some functions that make statistical calculations easy:
End of explanation
close = np.where(redshifts < 1)
print close
print redshifts[close]
middle = np.where( (redshifts>1) & (redshifts<2))
print redshifts[middle]
far = redshifts > 2
print far
print redshifts[far]
Explanation: We can use Numpy to select individual elements with certain properties. There are two ways to do this:
End of explanation
saveThisData = np.random.rand(20,2)    # This prints out random numbers 
#                                         between 0 and 1 in the shape we tell it
print saveThisData
print saveThisData.shape
saveThisData = np.reshape(saveThisData, (2,20))   # If we don't like the shape we can reshape it
print saveThisData
Explanation: Numpy is a great way to read in data from ASCII files, like the data files you got from the 20 m robotic telescope. Let's start by creating a data file and then reading it in, both using Numpy.
End of explanation
np.savetxt('myData.txt', saveThisData)
Explanation: Save the array to a file:
End of explanation
readThisData = np.genfromtxt('myData.txt')
print readThisData
print readThisData[0]
print readThisData[:,0]
Explanation: Now we can use Numpy to read the file into an array:
End of explanation
import matplotlib.pyplot as plt
%matplotlib inline
Explanation: Matplotlib: "math" + "plotting" + "library" (I think)
Matplotlib is a Python plotting package which can do anything from simple plots to complicated ones that are used in scientific papers.
We need to import the matplotlib.pyplot library, which is generally written as "plt" in shorthand. The second line below, ("%matplotlib inline") tells ipython to make the plots in this notebook; otherwise, the plots will appear in new windows.
End of explanation
print readThisData
plt.scatter(readThisData[0], readThisData[1])
Explanation: Matplotlib can create all the plots you need. For instance, we can plot our readThisData array from the Numpy session, which was two columns with 5 numbers each. Let's plot those numbers. It only takes a single line of code:
End of explanation
plt.plot(readThisData[0], readThisData[1])
plt.plot(readThisData[0], readThisData[1], 'o')  # another way to make a scatter plot
Explanation: If we want lines to connect the points:
End of explanation
x = np.linspace(0, 2*np.pi, 50)
y = np.sin(x)
plt.plot(x, y)
Explanation: Here's another example of what Numpy and Matplotlib can do:
End of explanation
plt.plot(x, y, label='values')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.legend()
plt.title("trigonometry!")          # you can use double or single quotes
Explanation: Let's add some labels and titles to our plot:
End of explanation
plt.plot(x, y, 'o-', color='red', markersize=6, linewidth=2, label='values')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.title("trigonometry!")
Explanation: Let's change how this looks a little:
End of explanation
plt.hist(readThisData[0], bins=20)
Explanation: Here's another kind of plot we can do:
End of explanation
x = np.linspace(-5, 5)
y = np.exp(-x**2)
plt.semilogy(x, y)
Explanation: What if we want logarithmic axes? The easiest way is to use semilogx(), semilogy(), or loglog():
End of explanation
from IPython.display import Image
Image(filename='breakout.png')
data = np.genfromtxt('Skynet_57026_GradMap_Milky_Way_-_Third_Quad_11753_12572.spect.cyb.txt')
freq = data[:,0]
XL1 = data[:,1]
YR1 = data[:,2]
plt.plot(freq, XL1, 'blue', label='XL1')
plt.plot(freq, YR1, 'black', label='YR1', linestyle=':')
plt.xlim(1415,1428)
plt.ylim(100,350)
plt.xlabel('frequency (Hz)', size=14)
plt.ylabel('counts', size=14)
plt.legend()
plt.title('Milky Way third quadrant', size=14, weight='bold')
peak_index = np.argmax(XL1)
peak = freq[peak_index]
print peak
Explanation: When you're in the IPython Notebook, plots show up as soon as you make them. If, on the other hand, you're writing a separate script and running from the command line (by typing in "python script.py" where script.py is the name of the script you just wrote) OR typing everything into the command line, you'll need to explicitly tell python to plot it. The same three lines above would instead be:
x = np.linspace(-5,5)
y = np.exp(-x**2)
plt.semilogy(x, y)
plt.show()
There are a lot of other plots you can make with matplotlib. The best way to find out how to do something is to look at the gallery of examples: http://matplotlib.org/gallery.html
Breakout session
Plot data you took using the 20m Telescope over the weekend.
Save the "Spectrum data" ASCII file at http://tinyurl.com/nljjmdh. (This is just the second half of your observation called "GRADMAP MILKY WAY - THIRD QUAD".) Read in the file and plot the XL1 data.
Make your plot look exactly like the one below.
Find the frequency of the biggest peak.
Bonus: Find the frequency of the second, smaller peak.
End of explanation
<END_TASK>