Skip to content

Commit

Permalink
Begin working on SciPy.ipynb; added checkpoints folder for SciPy
Browse files Browse the repository at this point in the history
  • Loading branch information
nwhitsett committed Jan 20, 2025
1 parent 40d5ff3 commit 2dd7237
Show file tree
Hide file tree
Showing 6 changed files with 573 additions and 1 deletion.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 108 additions & 1 deletion ExoCore/Curriculum/Data_Structures/Scipy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,106 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# SciPy"
"# [SciPy](https://docs.scipy.org/doc/scipy-1.15.0/index.html#)\n",
"\n",
"<img src=\"../../Auxiliary_Files/Graphics/Data_Structures/scipy_logo.png\" alt=\"SciPy_Logo\" width=\"600\" height=\"300\">\n",
"\n",
"## Table of Contents\n",
"- [Introduction](#introduction)\n",
"- [Overview](#overview)\n",
"- [Arrays](#arrays)\n",
" - [Basic Properties](#basic-properites)\n",
" - [Array Creation Methods]()\n",
" - [Example: Analyzing a 2D Image](#example-analyzing-a-2d-image)\n",
" - [Handling NANS](#handling-nans)\n",
" - [Stacking and Exporting Arrays](#stacking-and-exporting-arrays)\n",
" - [Sorting & Searching](#sorting--searching)\n",
"- [Mathematical Routines](#mathematical-routines)\n",
" - [General Functions](#basic-mathematical-functions)\n",
" - [Statistics](#statistics)\n",
" - [Linear Algebra](#linear-algebra)\n",
" - [Simple Polynomial Regression Methods](#simple-polynomial-regression-methods)\n",
"- [Exercises](#exercises)\n",
" - [Problem 1: Creating an Array in Four Different Ways](#problem-1-creating-an-array-in-four-different-ways)\n",
" - [Problem 2: Handling NANs](#problem-2-handling-nans)\n",
" - [Problem 3: Modeling Ingress and Egress](#problem-3-modeling-ingress-and-egress)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction\n",
"\n",
"The [SciPy](https://docs.scipy.org/doc/scipy-1.15.0/index.html#) package contains a slew of different analysis methods and tools that are relevant in exoplanet research. This lesson will focus on these methods, and outline several use cases. \n",
"\n",
" <div class=\"alert alert-block alert-warning\">\n",
"\n",
"**NOTE**: SciPy has many high level functions and optimization routines that are largely out of scope of ExoCore. These include routines that may be relevant in other aspects of astrophysics research; if you are interested, check out all SciPy modules [here](https://docs.scipy.org/doc/scipy-1.15.0/reference/index.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before we begin, run the code block below to activate the interactive portions of this lesson:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import scipy\n",
"from jupyterquiz import display_quiz\n",
"import json\n",
"with open(\"../../Exercise_Solutions/Module_3/SciPy/Checkpoints/questions.json\", \"r\") as file:\n",
" questions=json.load(file)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constants Module\n",
"\n",
"The first module in SciPy is the Constants module. The utility is in the name; this contains many relevant constants relevant in science and mathematics."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The value of pi is: 3.141592653589793\n",
"The speed of light is: 299792458.0\n",
"The mass of the neutron is: 1.67492749804e-27\n"
]
}
],
"source": [
"## Evoke pi using scipy.constants.pi\n",
"\n",
"print(\"The value of pi is: \" + str(scipy.constants.pi))\n",
"\n",
"## Speed of light...\n",
"\n",
"c = scipy.constants.c\n",
"m_n = scipy.constants.m_n\n",
"print(\"The speed of light is: \" + str(c))\n",
"print(\"The mass of the neutron is: \" + str(m_n))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find a list of available units [here](https://docs.scipy.org/doc/scipy-1.15.0/reference/constants.html). All units are reported in [SI units](https://en.wikipedia.org/wiki/International_System_of_Units#:~:text=The%20SI%20comprises%20a%20coherent,candela%20(cd%2C%20luminous%20intensity))."
]
}
],
Expand All @@ -15,7 +114,15 @@
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
Expand Down
220 changes: 220 additions & 0 deletions ExoCore/Exercise_Solutions/Module_3/SciPy/Checkpoints/questions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
[
{
"question": "Which of the following will invert the elements of an array, `arr`?",
"type": "multiple_choice",
"answers": [
{
"answer": "arr = arr[-1]",
"correct": false,
"feedback": "Incorrect. This returns only the last element."
},
{
"answer": "arr = arr[::-1]",
"correct": true,
"feedback": "Correct!"
},
{
"answer": "arr = arr[1:]",
"correct": false,
"feedback": "Incorrect. This returns the same array, excluding the first value."
},
{
"answer": "arr = arr[3::-1]",
"correct": false,
"feedback": "Incorrect. This returns an array from the fourth element back to the first element."
}
]
},
{
"question": "Which list comprehension syntax will exclude all negative elements, and multiply the remaining elements in an array, `arr`, by 1.5 times?",
"type": "multiple_choice",
"answers": [
{
"answer": "arr = [x*1.5 for x in arr if x < 0]",
"correct": false,
"feedback": "Incorrect. Almost! This will exclude all positive elements, not negative."
},
{
"answer": "arr = [x/2 for x in arr if np.abs(x) <= 0]",
"correct": false,
"feedback": "Incorrect. This divides all elements by two, and doesn't exclude any values!"
},
{
"answer": "arr = [x*1.5 for x in arr if x > 0]",
"correct": true,
"feedback": "Correct!"
},
{
"answer": "arr = [x*1.5 for x in arr]",
"correct": false,
"feedback": "Incorrect. This gets close, but does not exclude negative values."
}
]
},
{
"question": "What conditional would you pass to check if a value, x, is NAN?",
"type": "multiple_choice",
"answers": [
{
"answer": "if x == np.nan",
"correct": false,
"feedback": "Incorrect. Since NANs are not a string, number, or int, checking exactness is not well defined."
},
{
"answer": "if np.isnan(x) == 'True'",
"correct": false,
"feedback": "Incorrect. The returned value form np.isnan() is a Bool, not a string."
},
{
"answer": "if np.isnan(x) == True",
"correct": true,
"feedback": "Correct!"
},
{
"answer": "if x == 'nan'",
"correct": false,
"feedback": "Incorrect. The RHS is a string, not a np.nan object. Additionally, '==' conditional does not work to check if a value is NAN."
}
]
},
{
"question": "Which array creation method will create a 4x30 array, with each element being `45`?",
"type": "multiple_choice",
"answers": [
{
"answer": "np.ones((4, 30), value=45)",
"correct": false,
"feedback": "Incorrect. While np.ones is on the right track and the dimensions are correct, passing `value=45` will not work."
},
{
"answer": "np.ones((4, 30))*45",
"correct": true,
"feedback": "Correct!."
},
{
"answer": "np.linspace((4, 30))*40",
"correct": true,
"feedback": "Incorrect. Linspace creates evenly spaced arrays from the start and end value."
},
{
"answer": "np.zeros((4,30)) + np.ones((4,30))",
"correct": false,
"feedback": "Incorrect. While this gives the right shape, the values will be 1s, and the zero array is unnecessary."
}
]
},
{
"question": "np.argmax(array) will return the highest value in the array.",
"type": "multiple_choice",
"answers": [
{
"answer": "True",
"correct": false,
"feedback": "Incorrect. It will return the index of the highest value."
},
{
"answer": "False",
"correct": false,
"feedback": "Correct!"
}
]
},
{
"question": "What method allows you to take three arrays, arr1, arr2, arr3, and export them as a three columned csv file?",
"type": "multiple_choice",
"answers": [
{
"answer": "np.hstack((arr1, arr2, arr3)).T",
"correct": false,
"feedback": "Incorrect. hstack will stack all arrays into a single column."
},
{
"answer": "np.vstack((arr1,arr2,arr3))",
"correct": false,
"feedback": "Incorrect. Close, but you need to transpose."
},
{
"answer": "np.vstack((arr1,arr2,arr3)).T",
"correct": true,
"feedback": "Correct!"
},
{
"answer": "np.array((arr1,arr2,arr3))",
"correct": false,
"feedback": "Incorrect, this creates a new array with the three arrays inside of a tuple."
}
]
},
{
"question": "np.argmax(array) will return the highest value in the array.",
"type": "multiple_choice",
"answers": [
{
"answer": "True",
"correct": false,
"feedback": "Incorrect. It will return the index of the highest value."
},
{
"answer": "False",
"correct": false,
"feedback": "Correct!"
}
]
},
{
"question": "How do you call a random normal variate with mean 10 and variance 16?",
"type": "multiple_choice",
"answers": [
{
"answer": "np.random.normal(loc = 16, scale = 10)",
"correct": false,
"feedback": "Incorrect. `loc` specifies the mean, and scale specifies the standard deviation."
},
{
"answer": "np.random.normal(loc = 10, scale = 4)",
"correct": false,
"feedback": "Correct!"
},
{
"answer": "np.random.normal(loc = 10, scale = 16)",
"correct": true,
"feedback": "Incorrect. While the mean is correct, the scale is the standard deviation, not variance."
},
{
"answer": "np.random.random(loc = 10, scale = 4)",
"correct": false,
"feedback": "Incorrect. np.random.random creates a random variate between [0, 1), and does not specify the passed arguments."
}
]
},
{
"question": "NumPy has methods to support a quartic fit.",
"type": "multiple_choice",
"answers": [
{
"answer": "True",
"correct": false,
"feedback": "Correct!"
},
{
"answer": "False",
"correct": false,
"feedback": "Incorrect. This can be done using np.polynomial.polynomial.polyfit(x,y,4)"
}
]
},
{
"question": "In the system of equation below, what is the sum of x0, x1, and x2? Use np.linalg.solve.",
"type": "numeric",
"precision": 3,
"answers": [
{
"type": "value",
"value": "-0.125",
"correct": true,
"feedback": "Correct"
}

]
}
]
Loading

0 comments on commit 2dd7237

Please sign in to comment.